Skip to content

perf: cache date formatters - #65

Merged
lukeed merged 2 commits into
mainfrom
dates
Oct 15, 2026
Merged

lukeed merged 2 commits into
mainfrom
dates

Conversation

@lukeed

@lukeed lukeed commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #62. Stacked on #64.

$utils.date.format(value, options) made a new Intl.DateTimeFormat on each call with options, and making one is slow. It now keeps one formatter for each $site.lang and set of options. The key has the options in key order, so { month, year } and { year, month } share one. Content can choose the options, so it keeps at most 100 formatters, and drops the oldest first. Each call gives the same text as before.

The bench site now formats its dates with $utils.date.format, as a real site would, and not with its own helper. Its output does not change.

Benchmark

20,000 calls with { dateStyle: 'long' }: about 570 ms before, about 17 ms now.

bun run bench site, interleaved A/B against #63 on an M1 Max with Bun 1.4.3: two rounds each, median of 5 runs per round. Both sides used the same bench site, with $utils.date.format in its date partial.

10k-page site #63 this PR change
wall 5.81 s, 5.84 s 5.01 s, 5.01 s −14%
user 4.42 s, 4.43 s 3.89 s, 3.89 s −12%
peak RSS 722 MB, 738 MB 640 MB, 587 MB −11 to −20%
rendering pages stage 1.99 s, 1.99 s 1.41 s, 1.44 s

The build writes the same bytes.

Tests: 2 new tests, for one formatter across key orders and for the limit of 100. All 808 pass, tsc -p . and tsc -p build are clean, and bun run build passes.

@lukeed
lukeed changed the base branch from stream to sizes October 15, 2026 18:31
@lukeed lukeed mentioned this pull request Oct 15, 2026
`$utils.date.format` made a new `Intl.DateTimeFormat` for each call
with options, and making one is slow: 20,000 calls took ~570 ms. It
now keeps one formatter for each locale and set of options, keyed with
the options in key order, so 20,000 calls take ~17 ms. Content can
choose the options, so it keeps at most 100 and drops the oldest
first. Each call gives the same text as before.

Closes #62
The bench site formatted its dates with its own `$utils.humanDate`, so
it never ran `$utils.date.format`. It now uses the built-in, as a real
site would, and writes the same bytes.
@lukeed
lukeed changed the base branch from sizes to main October 15, 2026 18:43
@lukeed
lukeed merged commit 1b6a385 into main Oct 15, 2026
1 of 3 checks passed
@lukeed
lukeed deleted the dates branch October 15, 2026 18:47
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.

utils: $utils.date.format makes a new Intl.DateTimeFormat on each call

1 participant