Skip to content

Commit

Permalink
Optimize Performance with Caching for Resolved Options and Parsed Loc…
Browse files Browse the repository at this point in the history
…ale 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]>
  • Loading branch information
nodify-at authored Sep 4, 2024
1 parent a7f126a commit 3e9983c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions benchmarks/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ function runDateTimeSuite() {
dt.toFormat("T");
Settings.resetCaches();
})
.add("DateTime#format in german", () => {
dt.setLocale("de-De").toFormat("d. LLL. HH:mm");
})
.add("DateTime#format in german and no-cache", () => {
dt.setLocale("de-De").toFormat("d. LLL. HH:mm");
Settings.resetCaches();
})
.add("DateTime#add", () => {
dt.plus({ milliseconds: 3434 });
})
Expand Down
14 changes: 11 additions & 3 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ function systemLocale() {
}
}

let intlResolvedOptionsCache = {};
function getCachedIntResolvedOptions(locString) {
if (!intlResolvedOptionsCache[locString]) {
intlResolvedOptionsCache[locString] = new Intl.DateTimeFormat(locString).resolvedOptions();
}
return intlResolvedOptionsCache[locString];
}

let weekInfoCache = {};
function getCachedWeekInfo(locString) {
let data = weekInfoCache[locString];
Expand Down Expand Up @@ -167,7 +175,7 @@ function supportsFastNumbers(loc) {
loc.numberingSystem === "latn" ||
!loc.locale ||
loc.locale.startsWith("en") ||
new Intl.DateTimeFormat(loc.intl).resolvedOptions().numberingSystem === "latn"
getCachedIntResolvedOptions(loc.locale).numberingSystem === "latn"
);
}
}
Expand Down Expand Up @@ -326,7 +334,6 @@ const fallbackWeekSettings = {
/**
* @private
*/

export default class Locale {
static fromOpts(opts) {
return Locale.create(
Expand All @@ -353,6 +360,7 @@ export default class Locale {
intlDTCache = {};
intlNumCache = {};
intlRelCache = {};
intlResolvedOptionsCache = {};
}

static fromObject({ locale, numberingSystem, outputCalendar, weekSettings } = {}) {
Expand Down Expand Up @@ -506,7 +514,7 @@ export default class Locale {
return (
this.locale === "en" ||
this.locale.toLowerCase() === "en-us" ||
new Intl.DateTimeFormat(this.intl).resolvedOptions().locale.startsWith("en-us")
getCachedIntResolvedOptions(this.intl).locale.startsWith("en-us")
);
}

Expand Down

0 comments on commit 3e9983c

Please sign in to comment.