Skip to content

Instantly share code, notes, and snippets.

@andreialecu
Last active June 26, 2021 01:08
Show Gist options
  • Save andreialecu/2388730f9bde365d934cb343ecc38102 to your computer and use it in GitHub Desktop.
Save andreialecu/2388730f9bde365d934cb343ecc38102 to your computer and use it in GitHub Desktop.
performance patch for @formatjs/[email protected]
diff --git a/src/abstract/FormatDateTimePattern.js b/src/abstract/FormatDateTimePattern.js
index 47fd27397f6fff014af1c4b8862e26fea11e7aab..8d0d1234d9a5dcbd6131bbe206d4655a6c153726 100755
--- a/src/abstract/FormatDateTimePattern.js
+++ b/src/abstract/FormatDateTimePattern.js
@@ -32,6 +32,28 @@ function offsetToGmtString(gmtFormat, hourFormat, offsetInMs, style) {
}
return gmtFormat.replace('{0}', offsetStr);
}
+var numberFormatsCache = new Map();
+function getNumberFormatsForLocale(locale, fractionalSecondDigits) {
+ var cache = numberFormatsCache.get(locale);
+ if (cache)
+ return cache;
+ var nfOptions = Object.create(null);
+ nfOptions.useGrouping = false;
+ var nf = new Intl.NumberFormat(locale, nfOptions);
+ var nf2Options = Object.create(null);
+ nf2Options.minimumIntegerDigits = 2;
+ nf2Options.useGrouping = false;
+ var nf2 = new Intl.NumberFormat(locale, nf2Options);
+ var nf3 = undefined;
+ if (fractionalSecondDigits !== undefined) {
+ var nf3Options = Object.create(null);
+ nf3Options.minimumIntegerDigits = fractionalSecondDigits;
+ nf3Options.useGrouping = false;
+ nf3 = new Intl.NumberFormat(locale, nf3Options);
+ }
+ numberFormatsCache.set(locale, [nf, nf2, nf3]);
+ return [nf, nf2, nf3];
+}
/**
* https://tc39.es/ecma402/#sec-partitiondatetimepattern
* @param dtf
@@ -45,22 +67,8 @@ function FormatDateTimePattern(dtf, patternParts, x, _a) {
var dataLocale = internalSlots.dataLocale;
var dataLocaleData = localeData[dataLocale];
/** IMPL END */
- var locale = internalSlots.locale;
- var nfOptions = Object.create(null);
- nfOptions.useGrouping = false;
- var nf = new Intl.NumberFormat(locale, nfOptions);
- var nf2Options = Object.create(null);
- nf2Options.minimumIntegerDigits = 2;
- nf2Options.useGrouping = false;
- var nf2 = new Intl.NumberFormat(locale, nf2Options);
var fractionalSecondDigits = internalSlots.fractionalSecondDigits;
- var nf3;
- if (fractionalSecondDigits !== undefined) {
- var nf3Options = Object.create(null);
- nf3Options.minimumIntegerDigits = fractionalSecondDigits;
- nf3Options.useGrouping = false;
- nf3 = new Intl.NumberFormat(locale, nf3Options);
- }
+ var _b = getNumberFormatsForLocale(internalSlots.locale, fractionalSecondDigits), nf = _b[0], nf2 = _b[1], nf3 = _b[2];
var tm = ToLocalTime_1.ToLocalTime(x,
// @ts-ignore
internalSlots.calendar, internalSlots.timeZone, { tzData: tzData });
@andreialecu
Copy link
Author

There's a gap in the specification that results in some polyfills (formatjs in particular) being incredibly slow: tc39/ecma402#563 This has a PR at: tc39/ecma402#569

This results in this issue with the Hermes JS engine on React Native, especially on Android. facebook/hermes#23 (comment)

Here's one way to fix it: formatjs/formatjs#2827

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment