Skip to content

Commit

Permalink
feat: optimize performance by adding caching for resolved options and…
Browse files Browse the repository at this point in the history
… 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 <21654050+nodify-at@users.noreply.github.com>
  • Loading branch information
nodify-at committed Jun 27, 2024
1 parent cea7b5f commit 9e2011b
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ function systemLocale() {
}
}

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

let weekInfoCache = {};
function getCachedWeekInfo(locString) {
let data = weekInfoCache[locString];
Expand All @@ -73,7 +81,18 @@ function getCachedWeekInfo(locString) {
return data;
}

function parseLocaleString(localeStr) {
const localeStringCache = {};
function getParsedCachedLocaleString(localeStr) {
if (localeStringCache[localeStr]) {
return localeStringCache[localeStr];
}

const result = _parseLocaleString(localeStr);
localeStringCache[localeStr] = result;
return result;
}

function _parseLocaleString(localeStr) {
// I really want to avoid writing a BCP 47 parser
// see, e.g. https://github.com/wooorm/bcp-47
// Instead, we'll do this:
Expand Down Expand Up @@ -110,7 +129,13 @@ function parseLocaleString(localeStr) {
}
}

const intlConfigCache = {};
function intlConfigString(localeStr, numberingSystem, outputCalendar) {
const key = `${localeStr}${numberingSystem}${outputCalendar}`;
if (intlConfigCache[key]) {
return intlConfigCache[key];
}

if (outputCalendar || numberingSystem) {
if (!localeStr.includes("-u-")) {
localeStr += "-u";
Expand All @@ -123,10 +148,9 @@ function intlConfigString(localeStr, numberingSystem, outputCalendar) {
if (numberingSystem) {
localeStr += `-nu-${numberingSystem}`;
}
return localeStr;
} else {
return localeStr;
}
intlConfigCache[key] = localeStr;
return localeStr;
}

function mapMonths(f) {
Expand Down Expand Up @@ -160,16 +184,12 @@ function listStuff(loc, length, englishFn, intlFn) {
}

function supportsFastNumbers(loc) {
if (loc.numberingSystem && loc.numberingSystem !== "latn") {
return false;
} else {
return (
loc.numberingSystem === "latn" ||
!loc.locale ||
loc.locale.startsWith("en") ||
new Intl.DateTimeFormat(loc.intl).resolvedOptions().numberingSystem === "latn"
);
}
return loc.numberingSystem && loc.numberingSystem !== "latn"
? false
: loc.numberingSystem === "latn" ||
!loc.locale ||
loc.locale.startsWith("en") ||
resolvedOptions(loc.locale).numberingSystem === "latn";
}

/**
Expand Down Expand Up @@ -326,7 +346,6 @@ const fallbackWeekSettings = {
/**
* @private
*/

export default class Locale {
static fromOpts(opts) {
return Locale.create(
Expand Down Expand Up @@ -360,7 +379,8 @@ export default class Locale {
}

constructor(locale, numbering, outputCalendar, weekSettings, specifiedLocale) {
const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] = parseLocaleString(locale);
const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] =
getParsedCachedLocaleString(locale);

this.locale = parsedLocale;
this.numberingSystem = numbering || parsedNumberingSystem || null;
Expand All @@ -374,14 +394,14 @@ export default class Locale {
this.eraCache = {};

this.specifiedLocale = specifiedLocale;
this.fastNumbersCached = null;
this.fastNumbersCached = supportsFastNumbers(this);
this.isEnglishCached =
this.locale === "en" ||
this.locale.toLowerCase() === "en-us" ||
resolvedOptions(this.locale).locale.startsWith("en-us");
}

get fastNumbers() {
if (this.fastNumbersCached == null) {
this.fastNumbersCached = supportsFastNumbers(this);
}

return this.fastNumbersCached;
}

Expand Down Expand Up @@ -503,11 +523,7 @@ export default class Locale {
}

isEnglish() {
return (
this.locale === "en" ||
this.locale.toLowerCase() === "en-us" ||
new Intl.DateTimeFormat(this.intl).resolvedOptions().locale.startsWith("en-us")
);
return this.isEnglishCached;
}

getWeekSettings() {
Expand Down

0 comments on commit 9e2011b

Please sign in to comment.