Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Performance with Caching for Resolved Options and Parsed Locale Strings #1642

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be loc.intl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks a bit strange, because in my last commit I reverted this change. getCachedIntResolvedOptions function is used only in supportsFastNumbers and isEnglish .

See the code in master: https://github.com/moment/luxon/blob/master/src/impl/locale.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, I see your pointed code better now, sorry and you're right it should be intl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
}
}
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