Cache Intl.* constructors #215
Labels
area: performance
contributions welcome
Good for people looking to contribute
enhancement
New feature or request
Is your feature request related to a problem? Please describe.
It seems like on every call to
format.number
, or similar, there's a call to the correspondingIntl.NumberFormat
constructor. This is potentially expensive: A microbenchmark that I constructed suggests a 97% performance decrease (see https://jsbench.me/09lf9t531d/1). An older blog post suggests a more dramatic performance difference: https://blog.david-reess.de/posts/hBEx9w-on-number-formatting-and-performance.As someone coming from FormatJS, whose documentation emphasizes the use of
createIntlCache
(see https://formatjs.io/docs/intl), I was a bit surprised to see that this optimization is not present innext-intl
.Admittedly, on modern hardware like my M2 MacBook Pro, the absolute performance seems OK for both cases, even if there's a 49x relative difference.
Describe the solution you'd like
I'd suggest caching the construction of
Intl.*Format
objects between calls, perhaps by thecreateFormatter
function, or in the context.Admittedly, this would add complexity and size to the library, for the caching and memoization functionality, and might not be considered worth it.
Describe alternatives you've considered
Alternatively, if this is a known trade-off, I think it'd be good to mention in the documentation that it's been made. For example, in the FAQ section "How is this library different from using react-intl?".
The text was updated successfully, but these errors were encountered: