perf: format Date values in plain string schemas without toISOString #929
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
mcollina
This makes a
Datein a{ type: 'string' }schema (noformat) 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' }withoutformat: 'date-time'. For that case the generated code still callstoISOString()(index.js:1519), and so does the string array fallback inasPlainStringArray(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: theDatebranch callsasDateTime()instead oftoISOString().lib/serializer.js: I moved theDatepart ofasDateTimeinto a small module-level function,asDateTimeString(), soasPlainStringArraycan use it too. (asPlainStringArrayis called withoutthis, so it can't callthis.asDateTime.)benchmark/bench.js: two new cases for aDatein a plain string.Years outside 0-9999 and invalid dates still go through
toISOString(), so those results and theRangeErrorstay the same.One thing changes: if someone overrides
toISOString()(aDatesubclass, or a patchedDate.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:cmpagainstmain.Tests
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']andanyOf- the same for a long string array
- a
Datesubclass that overridestoISOString()main.npm testpasses with 100% coverage, and lint is clean.Questions
toISOString()no longer being called here, the same as forformat: 'date-time'?objectMechanism: 'native'(lib/projection.js) still usestoISOString(). I'd do that in a follow-up after #925 lands. Is that OK, or do you want it in this PR?README.mdsays "Date->stringviatoISOString()". 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
npm run test && npm run benchmark --if-presentand the Code of conduct