Skip to content

perf: faster objectMechanism 'native' for Date values and small arrays - #935

Merged
mcollina merged 1 commit into
mainfrom
perf/native-dates
Nov 21, 2026
Merged

mcollina merged 1 commit into
mainfrom
perf/native-dates

Conversation

@Tony133

@Tony133 Tony133 commented Nov 21, 2026

Copy link
Copy Markdown
Member

Proposal:

  • Make objectMechanism: 'native' faster when the data contains Date values.
  • Skip a useless write on the arrays reused by objectMechanism: 'native'.
  • The default mechanism is not touched.

Problem:

With objectMechanism: 'native', a Date was converted with toISOString() and then passed to JSON.stringify. On Node 26 this path is slower than the default mechanism: 41.8k ops/sec against 52.1k for a list of 50 rows with one date-time column. So the option made responses with dates slower instead of faster.

There was also a small difference in behaviour. Since #929 the default mechanism ignores a toISOString() overridden on a Date subclass, while objectMechanism: 'native' still called it for a string without format.

Change:

  • lib/serializer.js: new asDateTimeValue(date), which builds the ISO 8601 string with a single String.fromCharCode(...) call. JSON.stringify is much faster on a string built this way than on the one returned by toISOString(). Years outside 0-9999 and invalid dates still go through toISOString(), so the output and the error are the same as before.
  • lib/projection.js: date-time fields and string fields without format use asDateTimeValue for a Date.
  • lib/projection.js: the length of a reused array is written only when it changes. The assignment has a cost even when the value is the same.

Tests:

  • Date values in date-time fields and in string fields without format, as a property, as a nullable type and inside an array: objectMechanism: 'native' gives the same output as the default mechanism. The cases include year 0 and year 9999, years outside that range, an invalid date and a Date subclass that overrides toISOString().
  • Arrays that keep their length, grow, shrink and become null between two calls.
  • Coverage is 100% on Node 22, 24 and 26.

Benchmark:

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

Case JSON.stringify fast-json-stringify v7.3.8 (default mechanism) fast-json-stringify v7.3.8 (objectMechanism native) This PR (objectMechanism native) Difference This PR vs default mechanism This PR vs JSON.stringify
50 rows x 8 columns, 1 date-time 26.6k 52.1k 41.8k 111.8k +167.7% +114.4% +320.7%
50 rows x 8 columns, 2 date-time 15.2k 35.1k 20.4k 55.3k +171.1% +57.8% +263.7%
50 rows x 8 columns, 2 Date in a string without format 15.4k 34.8k 20.4k 54.5k +167.3% +56.4% +254.3%
user object with 1 date-time 679.6k 1.89M 1.54M 3.01M +95.7% +58.9% +342.8%
user object, array of 3 strings, no date 4.80M 2.65M 4.12M 4.40M +6.8% +66.0% -8.2%
control: 50 rows x 8 columns, no date 96.3k 56.0k 91.9k 91.7k -0.3% +63.8% -4.8%

Note:

  • semver-patch: no API change, the default mechanism is not modified.
  • objectMechanism: 'native' is active only on Node 25+, so nothing changes on Node 22 and 24.
  • The only visible change is for a Date subclass that overrides toISOString() in a string field without format: objectMechanism: 'native' now ignores the override, as the default mechanism does since perf: format Date values in plain string schemas without toISOString #929.

@Tony133
Tony133 marked this pull request as ready for review November 21, 2026 10:59

@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

@mcollina
mcollina merged commit c0d3e48 into main Nov 21, 2026
22 checks passed
@mcollina
mcollina deleted the perf/native-dates branch November 21, 2026 15:33
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.

2 participants