perf(@formatjs/intl-datetimeformat): cache number format instances in formatDateParts
#4529
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.
Closes #4510
Hey! As discussed in #4510, I noticed that calling
DateTimeFormat.formatDateParts()
creates new instances ofNumberFormat
on every call. As creatingNumberFormat
can be quite heavy due to thebestfit
mode ofLocaleMatcher
(see #4276 for more details), it's best to cacheNumberFormat
instances.To keep performance of the
DateTimeFormat
constructor as-is, I lazily initialize the three (sometimes just two)NumberFormat
instances during the first call toformatDateParts
and store them in theinternalSlots
of theDateTimeFormat
.I'm not 100% sure if
internalSlots
is the right place to store these internal instances, if not, let me know where I could store them instead.Thanks!