← Back to Pull requests

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

Closed
unknown closed this pull request without merging on Nov 27, 2026 13:20 UTC
View on GitHub
unknown unknown commented on Nov 23, 2026 10:50 UTC
Author

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 #938: the two branches merge without conflicts.

  • This comes before making the option the default in Fastify v6.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: gurgunday Review duration: 5909m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #940"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%