i18n: Add a caching layer to i18n-calypso numberFormat
to improve performance
#99110
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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