← Back to Pull requests

perf: format Date values in plain string schemas without toISOString #929

Closed
unknown closed this pull request without merging on Nov 20, 2026 15:44 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 15:02 UTC
Author

This makes a Date in a { type: 'string' } schema (no format) about 2.4x faster. A list of 50 rows with two dates each is about 3x faster. The output does not change.

Why

Most ORMs and DB drivers give timestamp columns back as a Date. A lot of schemas declare those columns as { type: 'string' } without format: 'date-time'. For that case the generated code still calls toISOString() (index.js:1519), and so does the string array fallback in asPlainStringArray (lib/serializer.js:215).

#865 added a faster formatter for format: 'date-time', but the plain string path never got it. This PR uses the same formatter there.

What changed

  • index.js: the Date branch calls asDateTime() instead of toISOString().

  • lib/serializer.js: I moved the Date part of asDateTime into a small module-level function, asDateTimeString(), so asPlainStringArray can use it too. (asPlainStringArray is called without this, so it can't call this.asDateTime.)

  • benchmark/bench.js: two new cases for a Date in a plain string.
  • Years outside 0-9999 and invalid dates still go through toISOString(), so those results and the RangeError stay the same.

    One thing changes: if someone overrides toISOString() (a Date subclass, or a patched Date.prototype), the override is not called any more. format: 'date-time' already works this way since #865. I added a test for it.

    Numbers

    Apple M3 Pro, the repo's bench runner, median ops/s (Node 24: 9 runs, Node 26: 5 runs):

    | case | Node 24 main → PR | Node 26 main → PR |
    |---|---|---|
    | Date in a string without format | 2.44M → 6.07M (2.49x) | 2.43M → 5.72M (2.36x) |
    | object with 2 Dates in strings | 1.18M → 2.62M (2.23x) | 1.17M → 2.49M (2.14x) |
    | formatted date-time (not changed) | -0.2% | -0.9% |

    Through build(schema)(input), median of 5 runs:

    | case | Node 24 main → PR | Node 26 main → PR |
    |---|---|---|
    | 50 rows { id, name, createdAt, updatedAt } | 25.3k → 75.3k (2.98x) | 26.9k → 76.0k (2.82x) |
    | 20 Dates in a string array | 143k → 443k (3.10x) | 143k → 448k (3.13x) |

    You can check it with npm run bench:cmp against main.

    Tests

  • New tests in test/date.test.js:

  • - edge years (0, 999, 9999, -1, 10000, the min and max Date) and an invalid Date, for type: 'string', nullable, ['string', 'null'] and anyOf
    - the same for a long string array
    - a Date subclass that overrides toISOString()
  • All new tests except the subclass one also pass on main.

  • npm test passes with 100% coverage, and lint is clean.
  • Questions

  • Are you OK with an overridden toISOString() no longer being called here, the same as for format: 'date-time'?

  • objectMechanism: 'native' (lib/projection.js) still uses toISOString(). I'd do that in a follow-up after #925 lands. Is that OK, or do you want it in this PR?

  • README.md says "Date -> string via toISOString()". The output is still the same, but should I update that line?
  • Related: #865, #900.

    I used Claude (Opus 5.5) to write and benchmark this change. I checked the results and the reasoning before opening it.

    <!--
    Thank you for your pull request. Please provide a description above and review
    the requirements below.

    Bug fixes and new features should include tests and possibly benchmarks.

    Contributors guide: https://github.com/fastify/fastify/blob/main/CONTRIBUTING.md

    By making a contribution to this project, I certify that:

    (a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

    (b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

    (c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

    (d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
    -->

    Checklist

  • run npm run test && npm run benchmark --if-present

  • tests and/or benchmarks are included

  • documentation is changed or added

  • commit message and code follows the Developer's Certification of Origin

  • and the Code of conduct

    Diff: 612 lines (+459 / -153) across 7 files Reviewer: mcollina Review duration: 1481m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 612 lines diff across 7 files
    0.50
    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.384
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #929"
    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: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    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: 19.8% · Safe to merge
    19.8%