← Back to Pull requests

perf: project with objectMechanism 'native' #937

Closed
unknown closed this pull request without merging on Nov 22, 2026 14:16 UTC
View on GitHub
unknown unknown commented on Nov 21, 2026 15:47 UTC
Author

Proposal:

  • Let objectMechanism: 'native' handle schemas that use $ref.

  • The default mechanism is not touched.
  • Problem:

    With objectMechanism: 'native', a single $ref anywhere in the schema makes the whole schema fall back to the default mechanism, without any notice. $ref is how schemas are shared, with definitions or with the schema option (what Fastify does with addSchema), so these users get no gain from the option. In the benchmark below the two fast-json-stringify v7.3.9 columns are the same for this reason.

    Change:

  • lib/projection.js resolves each $ref with the same function the default mechanism uses and projects the target as if it were written in place. It works for a definition of the same schema, for an external schema, for a $ref to another $ref and for a schema used more than once: each use has its own scratch.

  • Still left to the default mechanism: a recursive $ref; a $ref with other keywords next to it, which the default mechanism merges with the target (title, description and the other annotations are fine); a schema that would expand to more than 1000 nodes.

  • The pointer in the error messages now comes from the same Location as the default mechanism. This also fixes two small differences that are on main today: a key with / or ~ was not escaped in the message (#/properties/a/b instead of #/properties/a~1b), and a required key named like an Object.prototype member (constructor) that is not in properties was not checked.

  • The README section on objectMechanism lists the new cases.
  • Tests:

  • Schemas with $ref to definitions, to external schemas, to an anchor and to another $ref: objectMechanism: 'native' gives the same output or the same error as the default mechanism on more than 400 inputs.

  • Each use of the same definition keeps its own values, also inside an array.

  • Recursive $ref, $ref with sibling keywords, targets that are not projectable and a schema over the node limit keep the default mechanism, with the same output.

  • The two differences fixed above, and a $ref that cannot be found.

  • Coverage is 100% on Node 22, 24 and 26.
  • Benchmark:

    Node v26.13.0, Apple Silicon, ops/sec.

    | Case | JSON.stringify | fast-json-stringify v7.3.9 (default mechanism) | fast-json-stringify v7.3.9 (objectMechanism native) | This PR (objectMechanism native) | Difference | This PR vs default mechanism | This PR vs JSON.stringify |
    |---|---|---|---|---|---|---|---|
    | control: 50 rows x 8 columns, schema without $ref | 202.7k | 70.3k | 178.7k | 179.5k | +0.5% | +155.2% | -11.5% |
    | 50 rows x 8 columns, items is a $ref to a definition | 202.3k | 68.0k | 67.7k | 184.3k | +172.4% | +170.9% | -8.9% |
    | 50 rows x 8 columns, items is a $ref to a shared schema | 201.0k | 66.5k | 66.7k | 183.1k | +174.5% | +175.4% | -8.9% |
    | control: 50 rows x 10 columns, schema without $ref | 102.5k | 36.3k | 91.7k | 91.4k | -0.4% | +152.0% | -10.9% |
    | 50 rows x 10 columns, 2 of them a $ref to the same definition | 105.4k | 36.1k | 36.0k | 96.2k | +167.0% | +166.8% | -8.7% |
    | user object with 2 $ref to a shared schema | 3.96M | 1.84M | 1.84M | 3.48M | +89.2% | +89.1% | -12.1% |

    A schema with $ref is now as fast as the same schema written without it.

    Note:

  • semver-patch: no API change, same output as the default mechanism.

  • objectMechanism: 'native' is active only on Node 25+, so nothing changes on Node 22 and 24.

  • The limit of 1000 expanded nodes is a safety value against very large generated code, not a measured threshold.
  • Diff: 446 lines (+334 / -112) across 5 files Reviewer: gurgunday Review duration: 1349m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 446 lines diff across 5 files
    0.45
    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.365
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #937"
    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: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    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: 17.9% · Safe to merge
    17.9%