Skip to content

perf: project more schemas with objectMechanism 'native' - #936

Merged
mcollina merged 2 commits into
mainfrom
perf/native-coverage
Nov 21, 2026
Merged

mcollina merged 2 commits into
mainfrom
perf/native-coverage

Conversation

@Tony133

@Tony133 Tony133 commented Nov 21, 2026

Copy link
Copy Markdown
Member

Proposal:

  • Let objectMechanism: 'native' handle schemas that use nullable, enum, format or additionalProperties: false.
  • The default mechanism is not touched.

Problem:

With objectMechanism: 'native', one of these keywords anywhere in the schema is enough to make the whole schema fall back to the default mechanism, without any notice. They are very common in response schemas (format: 'email', nullable: true, additionalProperties: false), so many users who enable the option get no gain at all. In the benchmark below the two fast-json-stringify v7.3.8 columns are the same for this reason.

Change:

lib/projection.js now projects:

  • additionalProperties: false (the default mechanism treats it like a missing additionalProperties);
  • nullable: true on strings, numbers, integers, booleans, objects and arrays: null is written as null;
  • enum when the schema also has a type (the default mechanism does not use it to serialize);
  • any format on a string except unsafe, which writes the string without escaping; format on the other types, which the default mechanism ignores.

Still left to the default mechanism: $ref, const, anyOf / oneOf / allOf, if, patternProperties, ['integer', 'null'].

The README section on objectMechanism lists the new cases.

Tests:

  • A new test compares objectMechanism: 'native' with the default mechanism on more than 3000 combinations of schema and value: same output or same error. Each type is tested with and without nullable, with enum, format and default, as a property, as a required property and as an array item, with null, missing values, wrong types, toJSON() and undeclared properties.
  • The lists of projectable and not projectable schemas in the existing tests are updated.
  • 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.8 (default mechanism) fast-json-stringify v7.3.8 (objectMechanism native) This PR (objectMechanism native) Difference This PR vs default mechanism This PR vs JSON.stringify
50 rows x 8 columns, 2 string formats (uuid, email) 88.9k 48.9k 49.1k 84.6k +72.3% +73.1% -4.8%
50 rows x 8 columns, 1 enum 88.8k 49.1k 49.4k 84.8k +71.7% +72.8% -4.6%
50 rows x 8 columns, 2 nullable strings, no null in the data 89.7k 48.9k 49.2k 85.3k +73.4% +74.2% -4.9%
50 rows x 8 columns, 2 nullable strings, null in 1 row out of 3 89.5k 50.6k 50.6k 85.1k +68.2% +68.2% -5.0%
50 rows x 8 columns, all of the above and additionalProperties: false 90.3k 50.3k 50.4k 85.5k +69.7% +69.9% -5.2%
user object, all of the above, nullable nested object 4.21M 2.00M 2.02M 3.44M +70.4% +71.6% -18.3%
control: 50 rows x 8 columns, none of them 89.2k 49.2k 83.6k 83.9k +0.3% +70.4% -5.9%

Note:

@Tony133
Tony133 marked this pull request as ready for review November 21, 2026 11:27

@gurgunday gurgunday 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
@mcollina
mcollina merged commit 62dd959 into main Nov 21, 2026
22 checks passed
@mcollina
mcollina deleted the perf/native-coverage branch November 21, 2026 15:15
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