Repository navigation
perf(templates): reuse the date formatter in the blog templates - #5032
Conversation
formatDate called toLocaleDateString, which builds a new Intl formatter on every call. Post lists render one date per card, so on the post list and category pages this was about 9% of render CPU in a V8 profile of a blog with 507 posts. A cached Intl.DateTimeFormat per locale and month style produces the same output.
|
There was a problem hiding this comment.
Caching one Intl.DateTimeFormat per locale and month style is a sound, scoped fix for the per-post-card formatter cost. Both reviewers found the locale/month cache key and the invalid-date guard preserve existing output, and all eleven copies of locale.ts are updated consistently. One reviewer found a repository-convention problem: the cache map is module-local, but AGENTS.md requires module-scope singletons to live on globalThis under a Symbol.for key. Vite can duplicate modules across SSR chunks, so each copy would get its own map and rebuild formatters. The other reviewer approved and raised only an optional, non-line-specific follow-up: getSiteLocale() still calls Intl.DateTimeFormat.supportedLocalesOf on every formatDate call. Keying the cache on the raw defaultLocale would skip that call if profiling shows it matters. Both reviews were static; tests and performance were not rerun.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
| } | ||
| } | ||
|
|
||
| const dateFormats = new Map<string, Intl.DateTimeFormat>(); |
There was a problem hiding this comment.
[needs fixing] This module-local map does not follow AGENTS.md’s requirement that module-scope singletons live on globalThis under a Symbol.for key. Vite can duplicate modules across SSR chunks, giving each copy its own map and causing repeated formatter construction and separate warm-up across those chunks. Store the map using the prescribed global singleton pattern, retaining the locale/month key, and apply the same change to the Cloudflare template and demo/infra copies. The other reviewer did not raise this and approved the module-local cache as is.
Vite can duplicate a module across SSR chunks, so the formatter map now lives on globalThis under a Symbol.for key like the other module-scope caches.
There was a problem hiding this comment.
Caching one Intl.DateTimeFormat per locale and month style is a sound, scoped fix for the per-post-card formatter cost. Both reviewers approved and found no remaining issues. The earlier finding is resolved: all eleven copies of locale.ts now keep the cache on globalThis under a Symbol.for key, as AGENTS.md requires. Locale changes, long and short month styles, the English fallback and invalid-date handling remain covered by tests. The only optional follow-up is that getSiteLocale() still calls supportedLocalesOf on every formatDate call, which matters only if profiling shows it does. Both reviews were static; tests and performance were not rerun.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
What does this PR do?
The blog templates format each post date with
toLocaleDateString, which builds a newIntl.DateTimeFormaton every call. Post lists render one date per card, so on a blog with 507 posts this was about 9% of render CPU on/postsand category pages in a V8 profile. This PR keeps one formatter per locale and month style informatDateand reuses it. The output is the same, includingInvalid Datefor an invalid date, whereIntl.DateTimeFormat#formatwould throw instead.The change is in
templates/blogandtemplates/blog-cloudflare, plus the demos and infra sites that carry a copy of the sameutils/locale.ts. A new case inblog-locale.test.tschecks that a locale change between calls still produces the new locale's format, so the cache cannot pin the first locale it saw, and another that an invalid date still renders asInvalid Date.I run EmDash sites that render inside a Durable Object, where page renders share one thread, so CPU per render limits how many cache misses a site can serve at once.
Type of change
Your connection to this change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.No admin strings, no UI change. No changeset: the templates and demos are private packages listed in
.changeset/config.jsonignore.AI assistance disclosure
Screenshots / test output
Not applicable (no UI change). Measurements:
en-USlong date, 200,000 calls:toLocaleDateString32.7 µs, cached formatter 0.83 to 0.89 µs.