Skip to content

fix: keep objectMechanism 'native' identical to the default codegen - #925

Merged
Tony133 merged 4 commits into
mainfrom
fix/native-codegen-parity
Nov 21, 2026
Merged

Tony133 merged 4 commits into
mainfrom
fix/native-codegen-parity

Conversation

@Tony133

@Tony133 Tony133 commented Nov 10, 2026

Copy link
Copy Markdown
Member

Proposal:

  • Fix four cases where objectMechanism: 'native' does not produce the same output as the default codegen.

Problem:

objectMechanism: 'native' is documented as byte-identical to the default codegen. I ran the two mechanisms against each other on a wider set than the existing differential test (14 leaf schemas, each as a property, a required property, an array item and a property of an object in a list, with 36 awkward inputs) and found four cases where they differ.

  1. Re-entrancy. The scratch objects are shared by every call of a serializer. If a getter or a toJSON() of the input calls the same serializer, the inner call overwrites the values the outer call has already projected, and the outer output mixes the two objects:

    const stringify = fjs(schema, { objectMechanism: 'native' })
    stringify({
      a: 'outer-a',
      get b () { stringify({ a: 'INNER-a', b: 'INNER-b' }); return 'outer-b' }
    })
    // default codegen: {"a":"outer-a","b":"outer-b"}
    // native (v7.3.5):  {"a":"INNER-a","b":"outer-b"}
  2. format: 'date' and format: 'time' given as a string. The string is escaped twice: 'a"b' is serialized as "a\\\"b" instead of "a\"b".

  3. type: 'integer' given as a BigInt outside the safe integer range. The value goes through Number() and loses its low digits: 2n ** 64n is serialized as 18446744073709552000 instead of 18446744073709551616.

  4. A required property with a default. When the value is missing the default codegen uses the default, the projection throws "x" is required!.

Change:

All in lib/projection.js.

  1. Each serializer is now an instance that owns its scratch. A call that arrives while the instance is serializing is handed to a second instance, created on first use (and so on for deeper nesting). The flag is reset in a finally, so an error thrown by the input does not leave the serializer locked.
  2. A string given for date / time goes to the scratch as it is and is escaped once, by JSON.stringify.
  3. A BigInt inside the safe integer range is still converted to a number. Outside that range it stays a BigInt in the scratch, and that scratch is serialized by a small walker that writes every digit. The input is not read a second time. The native JSON.stringify path is unchanged for every other call.
  4. The default is checked before required, in the same order as the default codegen.

Tests:

test/object-mechanism-native.test.js: the new inputs are added to the existing differential test, plus three tests for required + default, BigInt array items and re-entrancy (nested up to three levels, a toJSON() on an array item, an error thrown inside the nested call and inside the outer call). The four groups fail on main and pass with this change.

Benchmark:

The hot path gains one boolean check and a try / finally, with no measurable cost. Node.js 26.12.0 (V8 14.6), Apple Silicon, objectMechanism: 'native' on both sides, median ops/sec over 15 alternating rounds, output consumed with Buffer.byteLength:

Case fast-json-stringify v7.3.5 (objectMechanism native) This PR (objectMechanism native) Difference
user profile response 875.6k 875.0k -0.1%
order response 115.1k 114.9k -0.2%
paginated products response 15.8k 15.8k -0.4%
README object (3 properties) 13.24M 13.14M -0.7%
50 rows, 3 integers each 441.2k 454.5k +3.0%

The first three rows are the benchmark/fixtures/api-responses schemas.

Note:

  • semver-patch. No option, type or documentation change: the outputs that change are the ones that were different from the default codegen.

@Tony133
Tony133 marked this pull request as ready for review November 10, 2026 15:39
@Tony133

Tony133 commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@mcollina @gurgunday when you both have a moment, could you please review this PR?

@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

@Tony133
Tony133 merged commit c522443 into main Nov 21, 2026
22 checks passed
@Tony133
Tony133 deleted the fix/native-codegen-parity branch November 21, 2026 10:45
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.

2 participants