Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cache Intl.* constructors #1193

Merged
merged 9 commits into from
Jul 12, 2024
Merged

Conversation

amannn
Copy link
Owner

@amannn amannn commented Jul 11, 2024

Fixes #215

Intl.* constructors like Intl.DateTimeFormat are now cached across components in order to improve rendering performance (see benchmark).

Details:

  • The message format cache that was already available has now been integrated with the cache for Intl.* constructors.
  • Cache lifetime:
    • Client: The cache is released when the locale prop of the provider changes. For long-running apps with a persistent provider at the root, this can reduce the memory footprint (e.g. in React Native).
    • Server: Lifetime of the request.
  • The .format calls are currently not cached. The reason is that while Intl.* constructor caching can work across various component instances and formatting calls, .format calls typically only affect a single call site. The overhead of memoization (serialization of args, diffing, bundle size) therefore might not be a good tradeoff here. In this area, avoiding re-renders is much more relevant, with the ideal solution being React Compiler.

Copy link

vercel bot commented Jul 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2024 0:21am
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2024 0:21am
next-intl-example-app-router-without-i18n-routing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2024 0:21am

@@ -63,6 +63,7 @@
"formatting"
],
"dependencies": {
"@formatjs/fast-memoize": "^2.2.0",
Copy link
Owner Author

Choose a reason for hiding this comment

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

Already in use due to intl-messageformat

@@ -120,27 +120,27 @@
"size-limit": [
{
"path": "dist/production/index.react-client.js",
"limit": "15.785 KB"
"limit": "16.055 KB"
Copy link
Owner Author

Choose a reason for hiding this comment

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

I've added a note to #779 to make sure we target modern browsers, currently we're compiling too much syntax (e.g. rest params).

@amannn amannn merged commit 52c4f2c into main Jul 12, 2024
6 checks passed
@amannn amannn deleted the feat/215-cache-intl-constructors branch July 12, 2024 12:30
amannn added a commit that referenced this pull request Aug 23, 2024
…seTranslations` is only used in Server Components (#1279)

Fixes a regression of #1193.

In case you're only using `useTranslations` in Server Components, this
will no longer bundle `intl-messageformat` on the client side (even if
you're using other functionality like `useFormatter` there).
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.

Cache Intl.* constructors
1 participant