-
Notifications
You must be signed in to change notification settings - Fork 2k
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
i18n: Add a caching layer to i18n-calypso numberFormat
to improve performance
#99110
Conversation
Link to live branch is being generated... |
numberFormat
numberFormat
to improve performance
Obviously, this is not mergeable. We can tweak the strategy and tests until we are confident, then I'll do the necessary cleanup / remove tests etc. |
It's nice that the caching provides a significant 10-20x speed improvement. Creating the After removing the performance experiments and keeping only the winning method, this is ready to be merged 🎉 |
Thanks for suggesting this! @jsnajdr I've added the method and some tests to #99538, which does some other refactors, like migrate the handlers to a separate location. If you get a chance to also review there it would be great! It references the work done here, so merging it should be fine to close this PR. |
Fixes https://github.com/Automattic/i18n-issues/issues/926
Proposed Changes
This is a follow-up to #98405 (comment) where we introduce Intl.NumberFormat to i18n-calypso's
numberFormat
method. In this PR we cache theIntl.NumberFormat
instance to avoid recreating on every call, which may be arbitrarily large due to components rerendering or just general use in the app. For the most part, usage-wise, the formatter is created with the same options.The idea I am exploring is to not checklist the
Intl.NumberFormat
options to a selected few, but rather create the index/cache from arbitrary options. Two strategies in this PR:json.stringify
on the options passed.json.stringify
.Benchmark
I've added some plain comparison tests in the form of a Jest suite. It compares simple use with a static set of options across 100K or so iterations. The results that I get, probably consistently:
json.stringify
fastest with~201
~220
~2245
So we get a whopping 10x performance improvement with a bit of caching.
json.stringify
seems most straight forward with lest complexity.Why are these changes being made?
Improve
numberFormat
(our go-to number formatter)Testing Instructions
yarn test-packages -- packages/i18n-calypso
Pre-merge Checklist