Repository navigation
perf: project with objectMechanism 'native' - #937
Merged
Merged
Conversation
Tony133
force-pushed
the
perf/native-ref
branch
from
November 21, 2026 15:48
7d49480 to
a6cc640
Compare
Tony133
marked this pull request as ready for review
November 21, 2026 15:52
gurgunday
reviewed
Nov 22, 2026
gurgunday
approved these changes
Nov 22, 2026
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposal:
objectMechanism: 'native'handle schemas that use$ref.Problem:
With
objectMechanism: 'native', a single$refanywhere in the schema makes the whole schema fall back to the default mechanism, without any notice.$refis how schemas are shared, withdefinitionsor with theschemaoption (what Fastify does withaddSchema), so these users get no gain from the option. In the benchmark below the two fast-json-stringify v7.3.9 columns are the same for this reason.Change:
lib/projection.jsresolves each$refwith the same function the default mechanism uses and projects the target as if it were written in place. It works for a definition of the same schema, for an external schema, for a$refto another$refand for a schema used more than once: each use has its own scratch.$ref; a$refwith other keywords next to it, which the default mechanism merges with the target (title,descriptionand the other annotations are fine); a schema that would expand to more than 1000 nodes.Locationas the default mechanism. This also fixes two small differences that are onmaintoday: a key with/or~was not escaped in the message (#/properties/a/binstead of#/properties/a~1b), and arequiredkey named like anObject.prototypemember (constructor) that is not inpropertieswas not checked.objectMechanismlists the new cases.Tests:
$refto definitions, to external schemas, to an anchor and to another$ref:objectMechanism: 'native'gives the same output or the same error as the default mechanism on more than 400 inputs.$ref,$refwith sibling keywords, targets that are not projectable and a schema over the node limit keep the default mechanism, with the same output.$refthat cannot be found.Benchmark:
Node v26.13.0, Apple Silicon, ops/sec.
A schema with
$refis now as fast as the same schema written without it.Note:
objectMechanism: 'native'is active only on Node 25+, so nothing changes on Node 22 and 24.