Repository navigation
perf: format Date values in plain string schemas without toISOString - #929
Merged
Merged
Conversation
A Date in a `type: 'string'` schema without `format` now uses the date-time formatter from fastify#865 instead of `toISOString()`, in the generated code and in the `asPlainStringArray` fallback.
iammerlijn
marked this pull request as draft
November 19, 2026 15:03
iammerlijn
marked this pull request as ready for review
November 19, 2026 15:10
Tony133
reviewed
Nov 20, 2026
Tony133
left a comment
Member
There was a problem hiding this comment.
Could you also update the README? The "Specific use cases" table still says Date is serialized "via toISOString()", which is no longer true after this PR.
Contributor
Author
@Tony133 Sure thing, I updated the table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
Through
build(schema)(input), median of 5 runs:{ id, name, createdAt, updatedAt }You can check it with
npm run bench:cmpagainstmain.Tests
test/date.test.js:type: 'string',nullable,['string', 'null']andanyOfDatesubclass 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 fix: keep objectMechanism 'native' identical to the default codegen #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.
Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct