Skip to content

fix: Cache Intl.DateTimeFormat instances for better performance#4141

Open
TrevorBurnham wants to merge 2 commits into
cloudscape-design:mainfrom
TrevorBurnham:format-date-localized-caching
Open

fix: Cache Intl.DateTimeFormat instances for better performance#4141
TrevorBurnham wants to merge 2 commits into
cloudscape-design:mainfrom
TrevorBurnham:format-date-localized-caching

Conversation

@TrevorBurnham

Copy link
Copy Markdown
Contributor

Description

Currently, every call to formatDateLocalized (used in DateInput) allocates new Intl.DateTimeFormat instances, which have significant initialization overhead. This PR adds caching for those formatters so that they're only created the first time formatDateLocalized is called.

How has this been tested?

I've added thorough unit test coverage.

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@TrevorBurnham TrevorBurnham requested a review from a team as a code owner December 31, 2025 16:48
@TrevorBurnham TrevorBurnham requested review from amanabiy and removed request for a team December 31, 2025 16:48
@TrevorBurnham TrevorBurnham force-pushed the format-date-localized-caching branch from b4e5960 to 1bbc389 Compare January 2, 2026 15:14
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.59%. Comparing base (3ad0425) to head (88ce863).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4141   +/-   ##
=======================================
  Coverage   97.59%   97.59%           
=======================================
  Files         950      951    +1     
  Lines       30642    30658   +16     
  Branches    11229    11230    +1     
=======================================
+ Hits        29906    29922   +16     
- Misses        689      729   +40     
+ Partials       47        7   -40     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

/**
* Cache for Intl.DateTimeFormat instances.
*
* Creating Intl.DateTimeFormat objects is expensive because the browser must

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.

How much performance gain are we expecting from this caching mechanism?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's tiny on a per-call basis: something on the order of 10-100 microseconds. But it's cumulative per localized date. On a page with a table full of formatted dates, the savings could add up to 10s of milliseconds.

@amanabiy amanabiy force-pushed the format-date-localized-caching branch from 2d5c154 to 88ce863 Compare July 10, 2026 08:44
Comment on lines +4 to +15
/**
* Cache for Intl.DateTimeFormat instances.
*
* Creating Intl.DateTimeFormat objects is expensive because the browser must
* parse locale data and resolve options. This cache stores formatter instances
* keyed by locale and options, allowing reuse across multiple format calls.
*
* The cache uses a simple Map with a string key derived from the locale and
* serialized options. Since the number of unique locale/option combinations
* in a typical application is small and bounded, we don't implement cache
* eviction.
*/

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.

The comment here is verbose, The file and function names are descriptive. Let's try to simplify the comment, removing redundancy. we could keep why we don't implement cache eviction (and keep it close to the formatterCache.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call. I've trimmed the docblocks down to just the why:

  • Top comment reduced to a two-line rationale for the cache existing.
  • Moved the no-eviction note directly onto the formatterCache declaration, as you suggested.
  • Dropped the getDateTimeFormat docblock since it only restated the signature.
  • Kept the one non-obvious line on createCacheKey (sorting keys for order-independent hits).

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