Skip to content

perf: enable objectMechanism 'native' on Node 22 and 24 - #938

Open
Tony133 wants to merge 3 commits into
mainfrom
perf/native-node22-24
Open

Tony133 wants to merge 3 commits into
mainfrom
perf/native-node22-24

Conversation

@Tony133

@Tony133 Tony133 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Proposal:

  • Use objectMechanism: 'native' also on Node 22 and 24, only for the schemas where at least half of the values are strings.
  • Nothing changes on Node 25+ and for the default mechanism.

Problem:

On Node 22 and 24 objectMechanism: 'native' does nothing, because V8 does not have the JSON.stringify fast path there. But the projection is still faster than the default mechanism on strings: the default mechanism escapes each string in JavaScript, the projection gives them to the native JSON.stringify. Most response schemas are made mostly of strings, so today these users get no gain on the two LTS lines.

Change:

  • index.js: three tiers instead of an on/off switch. Node 25+: every projectable schema, as today. Node 22 and 24: only the schemas made mostly of strings. Older runtimes: never.
  • lib/projection.js: isStringHeavy counts the values of the schema and how many of them are strings (['string', 'null'] and array items included). The schema is projected when the strings are at least half.
  • The rule also counts the values behind a $ref, as if they were written in place.
  • The internal forceProjection test option also accepts 'string-heavy', to test the Node 22 and 24 tier on every runtime.
  • README: the three tiers are listed in the objectMechanism section.

Tests:

  • Which schemas are projected on the current runtime, and each tier forced on every runtime.
  • Whatever the tier, the output is the same as the default mechanism.
  • The whole suite runs with the projection active on Node 22 and 24 for the string-heavy schemas. Coverage is 100% on Node 22, 24 and 26.

Benchmark:

The benchmarks were run again for this PR, on v7.3.9 and with more cases than in the comment on #906. They confirm what that comment found: the projection starts to win when half of the values are strings.

Apple Silicon, ops/sec. "Projection forced on every schema" is what would happen without the rule; the last column says what this PR uses.

Node v24.19.0

Case JSON.stringify fast-json-stringify v7.3.9 (objectMechanism native) This PR (objectMechanism native) Difference Projection forced on every schema Forced vs fast-json-stringify v7.3.9 This PR uses
object, 3 strings of 3 chars 7.26M 8.32M 6.95M -16.4% 6.75M -18.8% projection
object, 3 strings of 12 chars 6.35M 5.99M 6.04M +0.8% 5.94M -0.8% projection
object, 3 strings of 40 chars 4.99M 3.37M 4.75M +40.9% 4.76M +41.1% projection
object, 8 strings of 3 chars 3.75M 3.93M 3.69M -6.2% 3.54M -10.1% projection
object, 8 strings of 12 chars 3.32M 2.56M 3.15M +23.2% 3.15M +23.2% projection
object, 24 properties, no string 1.71M 2.64M 2.61M -1.4% 1.64M -38.0% default mechanism
object, 24 properties, 6 strings of 12 chars 1.55M 1.77M 1.81M +2.4% 1.47M -16.7% default mechanism
object, 24 properties, 12 strings of 12 chars 1.46M 1.35M 1.38M +2.6% 1.39M +2.8% projection
object, 24 properties, 18 strings of 12 chars 1.36M 1.10M 1.29M +17.5% 1.29M +17.0% projection
object, 24 strings of 12 chars 1.32M 896.7k 1.24M +37.8% 1.24M +37.8% projection
object, 4 strings of 200 chars 1.86M 1.14M 1.82M +59.4% 1.83M +60.3% projection
object, 4 strings that need escaping 5.35M 3.30M 5.22M +58.5% 5.07M +53.8% projection
object, 4 strings with non-ASCII chars 4.15M 2.88M 3.93M +36.2% 3.93M +36.3% projection
object, 2 strings and 1 date-time 1.32M 3.74M 4.40M +17.9% 4.35M +16.4% projection
order with 5 items (nested objects and arrays) 763.1k 600.2k 716.4k +19.4% 715.5k +19.2% projection
50 rows x 8 columns (5 strings) 93.8k 51.0k 89.5k +75.5% 87.1k +70.7% projection
50 rows x 8 columns, class instances 91.6k 49.1k 86.7k +76.5% 88.0k +79.1% projection
50 rows x 8 columns, 3 undeclared columns 20.9k 48.8k 84.1k +72.5% 85.1k +74.5% projection
1000 rows x 8 columns (5 strings) 4.1k 2.3k 3.9k +74.4% 3.9k +73.0% projection
50 rows x 6 columns, numbers and booleans only 126.4k 119.3k 107.3k -10.1% 121.1k +1.5% default mechanism
20 orders with 5 items each 41.7k 29.8k 38.8k +30.2% 39.1k +31.0% projection
object with an array of 1000 strings 54.9k 48.1k 51.3k +6.7% 51.5k +7.0% projection
object with an array of 1000 integers 148.0k 121.9k 126.9k +4.1% 127.5k +4.6% projection

Node v22.23.1

Case JSON.stringify fast-json-stringify v7.3.9 (objectMechanism native) This PR (objectMechanism native) Difference Projection forced on every schema Forced vs fast-json-stringify v7.3.9 This PR uses
object, 3 strings of 3 chars 8.80M 8.70M 8.35M -4.0% 8.15M -6.4% projection
object, 3 strings of 12 chars 8.01M 5.92M 7.44M +25.8% 7.46M +26.2% projection
object, 3 strings of 40 chars 6.34M 3.32M 5.99M +80.4% 6.00M +80.7% projection
object, 8 strings of 3 chars 4.49M 3.73M 4.23M +13.5% 4.16M +11.6% projection
object, 8 strings of 12 chars 4.03M 2.42M 3.76M +55.5% 3.77M +56.1% projection
object, 24 properties, no string 1.91M 2.74M 2.65M -3.0% 1.82M -33.3% default mechanism
object, 24 properties, 6 strings of 12 chars 1.78M 1.83M 1.77M -3.6% 1.68M -8.3% default mechanism
object, 24 properties, 12 strings of 12 chars 1.71M 1.35M 1.60M +19.0% 1.60M +18.6% projection
object, 24 properties, 18 strings of 12 chars 1.63M 1.07M 1.53M +42.7% 1.53M +42.6% projection
object, 24 strings of 12 chars 1.59M 874.7k 1.48M +69.0% 1.48M +69.3% projection
object, 4 strings of 200 chars 1.30M 1.16M 1.29M +11.2% 1.29M +11.8% projection
object, 4 strings that need escaping 6.92M 3.40M 6.54M +92.3% 6.35M +86.8% projection
object, 4 strings with non-ASCII chars 4.82M 3.58M 4.53M +26.6% 4.55M +27.1% projection
object, 2 strings and 1 date-time 1.34M 3.59M 4.91M +36.8% 4.92M +37.0% projection
order with 5 items (nested objects and arrays) 825.5k 576.0k 767.3k +33.2% 766.5k +33.1% projection
50 rows x 8 columns (5 strings) 102.5k 47.4k 97.5k +105.6% 94.5k +99.2% projection
50 rows x 8 columns, class instances 100.1k 46.6k 95.1k +104.3% 95.3k +104.7% projection
50 rows x 8 columns, 3 undeclared columns 22.5k 46.9k 93.5k +99.6% 93.8k +100.3% projection
1000 rows x 8 columns (5 strings) 4.7k 2.1k 4.4k +109.7% 4.4k +106.7% projection
50 rows x 6 columns, numbers and booleans only 120.5k 117.7k 103.3k -12.2% 115.4k -1.9% default mechanism
20 orders with 5 items each 45.0k 28.5k 41.3k +45.1% 41.4k +45.4% projection
object with an array of 1000 strings 60.5k 52.6k 56.1k +6.8% 55.8k +6.2% projection
object with an array of 1000 integers 104.4k 93.2k 97.8k +4.9% 98.0k +5.1% projection

What the tables say:

  • Lists of objects and nested objects gain the most: +72% to +77% on Node 24 and +100% to +110% on Node 22 on the lists, +19% to +45% on the orders.
  • The rule keeps the schemas with few strings on the default mechanism, where the projection would lose up to 38%.
  • One case is slower: small objects whose strings are very short (3 chars). -16.4% with 3 strings on Node 24, -4.0% on Node 22. From 12 chars the projection is never slower. The rule reads the schema, so it cannot see the length of the strings.
  • The row "50 rows x 6 columns, numbers and booleans only" uses the default mechanism, the same code as v7.3.9: its difference is noise of that case, not an effect of this PR.

Note:

  • semver-minor: objectMechanism: 'native' starts to work on Node 22 and 24. The output is the same as the default mechanism.
  • The option is still opt-in, so only who has set it is affected.
  • This is the question I asked in a comment on #906. Compared with that proposal, the rule is only "at least half of the values are strings". A list of objects made of numbers and booleans is not projected, because it does not gain (+1.5% on Node 24, -1.9% on Node 22 with the projection forced). The array length threshold from perf: project array items and let V8 stringify them #886 is not part of this PR.
  • As agreed in the comments, this goes out in a 7.x minor. Dropping Node 22 is left to the next major.
  • Rebased on top of perf: project with objectMechanism 'native' #937.

@mcollina

Copy link
Copy Markdown
Member

I think we should land this and drop node 22 in a new major.

@Tony133

Tony133 commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

I think we should land this and drop node 22 in a new major.

Good, I will take it out of draft once it is rebased on top of #937.

One question to be sure I understand: do you want this PR merged now in a 7.x minor and Node 22 dropped later in a separate major, or should this PR go into the new major together with the removal of Node 22? In the second case the rule would apply to Node 24 only and I can simplify the code here.

@mcollina

Copy link
Copy Markdown
Member

One question to be sure I understand: do you want this PR merged now in a 7.x minor and Node 22 dropped later in a separate major, or should this PR go into the new major together with the removal of Node 22? In the second case the rule would apply to Node 24 only and I can simplify the code here.

This can go out in a minor and the drop in the major.

@Tony133

Tony133 commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

One question to be sure I understand: do you want this PR merged now in a 7.x minor and Node 22 dropped later in a separate major, or should this PR go into the new major together with the removal of Node 22? In the second case the rule would apply to Node 24 only and I can simplify the code here.

This can go out in a minor and the drop in the major.

Ok, I am taking it out of draft as it is. It is already rebased on top of #937, and I updated the description.

@Tony133
Tony133 marked this pull request as ready for review November 22, 2026 15:54
Comment thread index.js
// test suite to exercise each tier on every runtime: true is the tier of
// Node 25+, 'string-heavy' the one of Node 22 and 24.
const projectionTier = options.forceProjection === true ? 2 : options.forceProjection === 'string-heavy' ? 1 : PROJECTION_TIER
if (context.objectMechanism === 'native' && projectionTier !== 0) {

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.

With largeArrayMechanism: 'json-stringify' the projection’s output differs from the default mechanism: on arrays at or above largeArraySize, undeclared properties are stripped and items are coerced. This already happens on Node 25+. With this PR it also reaches Node 22/24 users who set both options, and until now they got the default mechanism’s output:

const o = { largeArrayMechanism: 'json-stringify', largeArraySize: 2 }
build({ type: 'array', items: { type: 'string' } }, o)([1, 2, 3])                              // [1,2,3]
build({ type: 'array', items: { type: 'string' } }, { ...o, objectMechanism: 'native' })([1, 2, 3]) // ["1","2","3"]

Signed-off-by: Tony133 <a.tripodi133@gmail.com>

# Conflicts:
#	README.md

This branch has not been deployed

No deployments
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