Skip to content

Commit 3e9983c

Browse files
authored
Optimize Performance with Caching for Resolved Options and Parsed Locale Strings (#1642)
* feat: optimize performance by adding caching for resolved options and parsed locale strings - Implement caching to avoid redundant computations of resolved options and locale parsing. - Introduce a final `isEnglish` variable to efficiently check if the locale supports English. Signed-off-by: nodify-at <[email protected]> * feat: simplify caching logic for resolved options in supportsFastNumbers and isEnglish calls to improve performance Signed-off-by: nodify-at <[email protected]> * feat: add new benchmark tests to compare performance with and without Intl.resolvedOptions cache Signed-off-by: nodify-at <[email protected]> --------- Signed-off-by: nodify-at <[email protected]>
1 parent a7f126a commit 3e9983c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

benchmarks/datetime.js

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ function runDateTimeSuite() {
5858
dt.toFormat("T");
5959
Settings.resetCaches();
6060
})
61+
.add("DateTime#format in german", () => {
62+
dt.setLocale("de-De").toFormat("d. LLL. HH:mm");
63+
})
64+
.add("DateTime#format in german and no-cache", () => {
65+
dt.setLocale("de-De").toFormat("d. LLL. HH:mm");
66+
Settings.resetCaches();
67+
})
6168
.add("DateTime#add", () => {
6269
dt.plus({ milliseconds: 3434 });
6370
})

src/impl/locale.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ function systemLocale() {
6161
}
6262
}
6363

64+
let intlResolvedOptionsCache = {};
65+
function getCachedIntResolvedOptions(locString) {
66+
if (!intlResolvedOptionsCache[locString]) {
67+
intlResolvedOptionsCache[locString] = new Intl.DateTimeFormat(locString).resolvedOptions();
68+
}
69+
return intlResolvedOptionsCache[locString];
70+
}
71+
6472
let weekInfoCache = {};
6573
function getCachedWeekInfo(locString) {
6674
let data = weekInfoCache[locString];
@@ -167,7 +175,7 @@ function supportsFastNumbers(loc) {
167175
loc.numberingSystem === "latn" ||
168176
!loc.locale ||
169177
loc.locale.startsWith("en") ||
170-
new Intl.DateTimeFormat(loc.intl).resolvedOptions().numberingSystem === "latn"
178+
getCachedIntResolvedOptions(loc.locale).numberingSystem === "latn"
171179
);
172180
}
173181
}
@@ -326,7 +334,6 @@ const fallbackWeekSettings = {
326334
/**
327335
* @private
328336
*/
329-
330337
export default class Locale {
331338
static fromOpts(opts) {
332339
return Locale.create(
@@ -353,6 +360,7 @@ export default class Locale {
353360
intlDTCache = {};
354361
intlNumCache = {};
355362
intlRelCache = {};
363+
intlResolvedOptionsCache = {};
356364
}
357365

358366
static fromObject({ locale, numberingSystem, outputCalendar, weekSettings } = {}) {
@@ -506,7 +514,7 @@ export default class Locale {
506514
return (
507515
this.locale === "en" ||
508516
this.locale.toLowerCase() === "en-us" ||
509-
new Intl.DateTimeFormat(this.intl).resolvedOptions().locale.startsWith("en-us")
517+
getCachedIntResolvedOptions(this.intl).locale.startsWith("en-us")
510518
);
511519
}
512520

0 commit comments

Comments
 (0)