Skip to content

perf(templates): reuse the date formatter in the blog templates - #5032

Merged
swissky merged 4 commits into
emdash-cms:mainfrom
swissky:perf/blog-template-date-formatter
Nov 25, 2026
Merged

swissky merged 4 commits into
emdash-cms:mainfrom
swissky:perf/blog-template-date-formatter

Conversation

@swissky

@swissky swissky commented Nov 22, 2026

Copy link
Copy Markdown
Member

What does this PR do?

The blog templates format each post date with toLocaleDateString, which builds a new Intl.DateTimeFormat on every call. Post lists render one date per card, so on a blog with 507 posts this was about 9% of render CPU on /posts and category pages in a V8 profile. This PR keeps one formatter per locale and month style in formatDate and reuses it. The output is the same, including Invalid Date for an invalid date, where Intl.DateTimeFormat#format would throw instead.

The change is in templates/blog and templates/blog-cloudflare, plus the demos and infra sites that carry a copy of the same utils/locale.ts. A new case in blog-locale.test.ts checks 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 as Invalid 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

  • Bug fix
  • Feature (requires an accepted feature plan or RFC)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Your connection to this change

  • I use EmDash and ran into the problem this PR addresses
  • I maintain a tool, dependency, or service that EmDash uses, and this change concerns that integration
  • A Maintainer approved this specific change before I opened the PR

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to their merged design PR: https://github.com/emdash-cms/emdash/pull/...
  • I have included screenshots below if this PR changes the UI

No admin strings, no UI change. No changeset: the templates and demos are private packages listed in .changeset/config.json ignore.

AI assistance disclosure

  • This PR includes AI-generated code or text — model/tool: Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change). Measurements:

  • Per call, Node 22, en-US long date, 200,000 calls: toLocaleDateString 32.7 µs, cached formatter 0.83 to 0.89 µs.
  • Render in a Durable Object (Astro and SQLite in one DO, 507 posts, 7 pages as forced cache misses from Zurich, A and B deployed alternately three times each, 9,240 requests, all 200): at 20 requests in flight, the median throughput went from 48 to 66 requests per second (+37%) and p50 latency from 346 to 284 ms. With one request at a time there was almost no difference, because network time dominates there.

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.
@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: daee845

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread templates/blog/src/utils/locale.ts Outdated
}
}

const dateFormats = new Map<string, Intl.DateTimeFormat>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[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.
@github-actions github-actions Bot added size/L and removed size/M labels Nov 22, 2026
@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Nov 22, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Nov 22, 2026

@danielmlr danielmlr 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.

Thank you!

@emdashbot emdashbot Bot added review/needs-review No maintainer or bot review yet and removed review/needs-review No maintainer or bot review yet labels Nov 23, 2026
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Nov 23, 2026
@swissky
swissky merged commit 30911e9 into emdash-cms:main Nov 25, 2026
89 of 92 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants