Repository navigation
Conversation
|
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. |
6f2734a to
1e7ab2e
Compare
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. |
| // 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) { |
There was a problem hiding this comment.
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
Proposal:
objectMechanism: 'native'also on Node 22 and 24, only for the schemas where at least half of the values are strings.Problem:
On Node 22 and 24
objectMechanism: 'native'does nothing, because V8 does not have theJSON.stringifyfast 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 nativeJSON.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:isStringHeavycounts 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.$ref, as if they were written in place.forceProjectiontest option also accepts'string-heavy', to test the Node 22 and 24 tier on every runtime.objectMechanismsection.Tests:
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
Node v22.23.1
What the tables say:
Note:
objectMechanism: 'native'starts to work on Node 22 and 24. The output is the same as the default mechanism.