Skip to content

fix: objectMechanism 'native' keeps no data between calls - #940

Merged
mcollina merged 2 commits into
mainfrom
fix/native-memory
Nov 27, 2026
Merged

mcollina merged 2 commits into
mainfrom
fix/native-memory

Conversation

@Tony133

@Tony133 Tony133 commented Nov 23, 2026

Copy link
Copy Markdown
Member

Proposal:

  • objectMechanism: 'native' no longer keeps data in memory between calls.
  • Objects with more than 127 properties are left to the default mechanism, which is faster there.
  • The default mechanism is not touched.

Problem:

objectMechanism: 'native' copies the input into scratch objects that are reused between calls. They are never released: they keep the values of the last call, and the pool of an array grows to the longest array seen and never shrinks. A serializer that once writes a list of 100,000 rows keeps about 35 MB for as long as it lives, also when it then serves 10 rows. In a Fastify application with 300 routes, a few large responses take the heap from 26 MB to 163 MB, and it does not come back.

This is acceptable for an option that is set by hand. It is not for a default, which is what is being asked for Fastify v6 in fastify/fastify#6507.

A second problem: the JSON.stringify fast path of V8 handles objects with up to 127 properties. From 128 the option is 34% to 38% slower than the default mechanism.

Change:

  • lib/projection.js: each call builds new objects with the declared properties and drops them when it returns. Nothing is shared between calls, so there is no scratch, no pool, and no code for a call that starts while another one is running.
  • A BigInt outside the safe integer range is still written by the walker. The flag that selects it is shared by the calls of a serializer, so a call that loses it finds out because JSON.stringify throws.
  • A property named __proto__ is now written, as the default mechanism does. On main it was dropped.
  • Objects with more than 127 properties are not projected.
  • README: the objectMechanism section is updated.

Tests:

  • A serializer shares nothing between calls: missing properties after a full call, a null root, and a call made inside another one with a BigInt in the outer call, in the inner one and in both.
  • A property named __proto__, in the schema and in the input.
  • 127 and 128 properties, also nested and as the item of an array.
  • The existing suite is unchanged and passes. Coverage is 100% on Node 22, 24 and 26.

Benchmark:

Node v26.13.0, Apple Silicon.

Memory kept by the serializers after the call, with the input dropped and after a full garbage collection:

Scenario fast-json-stringify v7.3.10 (default mechanism) fast-json-stringify v7.3.10 (objectMechanism native) This PR (objectMechanism native)
one serializer, a list of 100,000 rows once 0.28 MB 35.21 MB 0.18 MB
same, then a list of 10 rows (the next request) 0.03 MB 34.44 MB 0.03 MB
one serializer, an object with a 20 MB string once 0.01 MB 20.01 MB 0.01 MB
same, then a short string (the next request) 0.00 MB 0.00 MB 0.00 MB
1,000 serializers, each called once with a list of 20 rows 0.53 MB 6.08 MB 0.70 MB
1,000 serializers, each called once with a list of 50 rows 0.40 MB 14.56 MB 0.62 MB
1,000 serializers, each called once with a list of 200 rows 0.40 MB 52.23 MB 0.62 MB
1,000 serializers, each called once with a list of 1,000 rows 0.00 MB 258.68 MB 0.62 MB

Heap of a Fastify application with 300 routes, after each phase of traffic:

Serializer After the ordinary traffic After the pages of 500 rows After the exports of 20,000 rows Back to the ordinary traffic
fast-json-stringify v7.3.10 (default mechanism) 21.5 MB 23.1 MB 23.3 MB 25.8 MB
fast-json-stringify v7.3.10 (objectMechanism native) 23.1 MB 48.7 MB 164.2 MB 163.4 MB
This PR (objectMechanism native) 22.3 MB 24.0 MB 24.1 MB 26.6 MB

Speed, ops/sec, each measurement in its own process:

Case JSON.stringify fast-json-stringify v7.3.10 (default mechanism) fast-json-stringify v7.3.10 (objectMechanism native) This PR (objectMechanism native) Difference This PR vs default mechanism This PR vs JSON.stringify
one object, 8 properties 6.04M 3.59M 5.37M 5.71M +6.4% +59.2% -5.5%
order with 5 items (nested objects and arrays) 1.63M 671.7k 1.38M 1.40M +1.5% +107.8% -14.5%
50 rows x 8 columns 203.8k 67.8k 177.2k 184.1k +3.9% +171.8% -9.6%
1000 rows x 8 columns 9.5k 3.6k 8.9k 9.3k +5.0% +161.4% -1.8%
10000 rows x 8 columns 1.0k 0.3k 0.9k 0.9k +7.5% +205.9% -4.2%
20 orders with 5 items each 91.8k 33.5k 78.6k 80.1k +1.9% +139.1% -12.7%
object with an array of 1000 strings 121.3k 93.7k 108.1k 113.9k +5.4% +21.6% -6.0%
object with an array of 1000 integers 263.1k 215.5k 238.7k 222.6k -6.7% +3.3% -15.4%

Objects with many properties, ops/sec:

Case JSON.stringify fast-json-stringify v7.3.10 (default mechanism) fast-json-stringify v7.3.10 (objectMechanism native) This PR (objectMechanism native) Difference This PR vs default mechanism
object, 64 properties 1.51M 514.5k 1.14M 1.12M -2.2% +116.8%
object, 100 properties 1.02M 333.2k 741.5k 721.6k -2.7% +116.6%
object, 127 properties 805.0k 256.4k 578.2k 563.5k -2.5% +119.8%
object, 128 properties 167.6k 214.2k 140.4k 215.8k +53.7% +0.8%
object, 300 properties 68.1k 89.7k 56.5k 90.2k +59.8% +0.6%
object, 500 properties 39.7k 54.4k 33.5k 54.8k +63.6% +0.8%

What the tables say:

  • The memory kept is the one of the default mechanism, in the single serializers and in the Fastify application.
  • Speed is the same or a little better: +1.5% to +7.5% in seven cases out of eight.
  • Two cases are slower: an array of 1000 integers (-6.7%, still +3.3% over the default mechanism) and objects with 64 to 127 properties (-2% to -3%).
  • From 128 properties the option is now as fast as the default mechanism, instead of 34% to 38% slower.

Note:

  • semver-patch: no API change, same output as the default mechanism.
  • It is a rewrite of the emitter in lib/projection.js, not a small fix: reusing the objects was the cause of the memory kept, and it turned out not to pay in speed.
  • Two commits, to be read one at a time: the first removes the scratch, the second adds the limit of 127 properties (a few lines). They are together because both are needed before the option can be a default, and the second is written on top of the first. If you prefer two PRs, I can split them.
  • Independent from perf: enable objectMechanism 'native' on Node 22 and 24 #938: the two branches merge without conflicts.
  • This comes before making the option the default in Fastify v6.

@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

@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

@mcollina
mcollina merged commit a06b3a4 into main Nov 27, 2026
22 checks passed
@mcollina
mcollina deleted the fix/native-memory branch November 27, 2026 13:20
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