Skip to content

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

Merged
Tony133 merged 2 commits into
fastify:mainfrom
iammerlijn:perf/date-plain-string
Nov 20, 2026
Merged

Tony133 merged 2 commits into
fastify:mainfrom
iammerlijn:perf/date-plain-string

Conversation

@iammerlijn

@iammerlijn iammerlijn commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 fix: keep objectMechanism 'native' identical to the default codegen #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.

Checklist

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
iammerlijn marked this pull request as draft November 19, 2026 15:03
@iammerlijn
iammerlijn marked this pull request as ready for review November 19, 2026 15:10

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@iammerlijn

iammerlijn commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@Tony133 Sure thing, I updated the table

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Tony133
Tony133 merged commit 189ec37 into fastify:main Nov 20, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants