Skip to content

perf: project with objectMechanism 'native' - #937

Merged
Tony133 merged 2 commits into
mainfrom
perf/native-ref
Nov 22, 2026
Merged

Tony133 merged 2 commits into
mainfrom
perf/native-ref

Conversation

@Tony133

@Tony133 Tony133 commented Nov 21, 2026

Copy link
Copy Markdown
Member

Proposal:

  • Let objectMechanism: 'native' handle schemas that use $ref.
  • The default mechanism is not touched.

Problem:

With objectMechanism: 'native', a single $ref anywhere in the schema makes the whole schema fall back to the default mechanism, without any notice. $ref is how schemas are shared, with definitions or with the schema option (what Fastify does with addSchema), 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.js resolves each $ref with 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 $ref to another $ref and for a schema used more than once: each use has its own scratch.
  • Still left to the default mechanism: a recursive $ref; a $ref with other keywords next to it, which the default mechanism merges with the target (title, description and the other annotations are fine); a schema that would expand to more than 1000 nodes.
  • The pointer in the error messages now comes from the same Location as the default mechanism. This also fixes two small differences that are on main today: a key with / or ~ was not escaped in the message (#/properties/a/b instead of #/properties/a~1b), and a required key named like an Object.prototype member (constructor) that is not in properties was not checked.
  • The README section on objectMechanism lists the new cases.

Tests:

  • Schemas with $ref to 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.
  • Each use of the same definition keeps its own values, also inside an array.
  • Recursive $ref, $ref with sibling keywords, targets that are not projectable and a schema over the node limit keep the default mechanism, with the same output.
  • The two differences fixed above, and a $ref that cannot be found.
  • Coverage is 100% on Node 22, 24 and 26.

Benchmark:

Node v26.13.0, Apple Silicon, ops/sec.

Case JSON.stringify fast-json-stringify v7.3.9 (default mechanism) fast-json-stringify v7.3.9 (objectMechanism native) This PR (objectMechanism native) Difference This PR vs default mechanism This PR vs JSON.stringify
control: 50 rows x 8 columns, schema without $ref 202.7k 70.3k 178.7k 179.5k +0.5% +155.2% -11.5%
50 rows x 8 columns, items is a $ref to a definition 202.3k 68.0k 67.7k 184.3k +172.4% +170.9% -8.9%
50 rows x 8 columns, items is a $ref to a shared schema 201.0k 66.5k 66.7k 183.1k +174.5% +175.4% -8.9%
control: 50 rows x 10 columns, schema without $ref 102.5k 36.3k 91.7k 91.4k -0.4% +152.0% -10.9%
50 rows x 10 columns, 2 of them a $ref to the same definition 105.4k 36.1k 36.0k 96.2k +167.0% +166.8% -8.7%
user object with 2 $ref to a shared schema 3.96M 1.84M 1.84M 3.48M +89.2% +89.1% -12.1%

A schema with $ref is now as fast as the same schema written without it.

Note:

  • semver-patch: no API change, same output as the default mechanism.
  • objectMechanism: 'native' is active only on Node 25+, so nothing changes on Node 22 and 24.
  • The limit of 1000 expanded nodes is a safety value against very large generated code, not a measured threshold.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread lib/projection.js Resolved
@Tony133
Tony133 requested a review from gurgunday November 22, 2026 10:50
@Tony133
Tony133 merged commit 005470f into main Nov 22, 2026
22 checks passed
@Tony133
Tony133 deleted the perf/native-ref branch November 22, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants