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

perf(input-time-zone): improve memory footprint by leveraging browser cache and releasing utils after their use #9945

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,6 @@ export function getUserTimeZoneName(): string {
return dateFormatter.resolvedOptions().timeZone;
}

/**
* The lazy-loaded timezone-groups lib to be used across instances.
*/
let offsetGroupUtils: Promise<
[
typeof import("timezone-groups/dist/groupByOffset/index.mjs"),
typeof import("timezone-groups/dist/groupByOffset/strategy/native.mjs"),
]
>;
let regionGroupUtils: Promise<
[
typeof import("timezone-groups/dist/groupByRegion/index.mjs"),
typeof import("timezone-groups/dist/utils/country.mjs"),
]
>;
let nameGroupUtils: Promise<typeof import("timezone-groups/dist/groupByName/index.mjs")>;

export async function createTimeZoneItems(
locale: SupportedLocale,
messages: InputTimeZoneMessages,
Expand All @@ -81,11 +64,7 @@ export async function createTimeZoneItems(
standardTime: OffsetStyle,
): Promise<TimeZoneItem[] | TimeZoneItemGroup[]> {
if (mode === "name") {
if (!nameGroupUtils) {
nameGroupUtils = import("timezone-groups/dist/groupByName/index.mjs");
}

return nameGroupUtils.then(async ({ groupByName }) => {
const { groupByName } = await import("timezone-groups/dist/groupByName/index.mjs");
const groups = await groupByName();

return groups
Expand All @@ -101,7 +80,6 @@ export async function createTimeZoneItems(
})
.filter((group) => !!group)
.sort();
});
}

const effectiveLocale =
Expand All @@ -114,14 +92,10 @@ export async function createTimeZoneItems(
const referenceDateInMs: number = referenceDate.getTime();

if (mode === "region") {
if (!regionGroupUtils) {
regionGroupUtils = Promise.all([
const [{ groupByRegion }, { getCountry }] = await Promise.all([
import("timezone-groups/dist/groupByRegion/index.mjs"),
import("timezone-groups/dist/utils/country.mjs"),
]);
}

return regionGroupUtils.then(async ([{ groupByRegion }, { getCountry }]) => {
const groups = await groupByRegion();

return groups
Expand All @@ -146,17 +120,13 @@ export async function createTimeZoneItems(
};
})
.sort((groupA, groupB) => groupA.label.localeCompare(groupB.label));
});
}

if (!offsetGroupUtils) {
offsetGroupUtils = Promise.all([
const [{ groupByOffset }, { DateEngine }] = await Promise.all([
import("timezone-groups/dist/groupByOffset/index.mjs"),
import("timezone-groups/dist/groupByOffset/strategy/native/index.mjs"),
]);
}

return offsetGroupUtils.then(async ([{ groupByOffset }, { DateEngine }]) => {
const groups = await groupByOffset({
dateEngine: new DateEngine(),
groupDateRange: 1,
Expand Down Expand Up @@ -202,7 +172,6 @@ export async function createTimeZoneItems(
})
.filter((group) => !!group)
.sort((groupA, groupB) => groupA.value - groupB.value);
});
}

function getTimeZoneLabel(timeZone: string, messages: InputTimeZoneMessages): string {
Expand Down
Loading