Skip to content
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
22 changes: 21 additions & 1 deletion app/web/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,29 @@ export function localizeDateTimeRange(
return format.formatRange(start, end);
}

/// gets in an Intl.DateTimeFormat based on params.
// Creating Intl.DateTimeFormat every time is 40x slower.
const intlDateTimeFormatCache = new Map<string, Intl.DateTimeFormat>();

/// Gets an Intl.DateTimeFormat based on params.
function getIntlDateTimeFormat(
args: LocalizeDateTimeParams,
): Intl.DateTimeFormat {
// We can't use args as the Map key as it uses reference equality.
// Convert it to a json string. The Symbol type requires special handling.
const cacheKey = JSON.stringify(args, (_, v) =>
typeof v === "symbol" ? v.toString() : v,
);
const cached = intlDateTimeFormatCache.get(cacheKey);
if (cached) return cached;

const format = createIntlDateTimeFormat(args);
intlDateTimeFormatCache.set(cacheKey, format);
return format;
}

/// Creates a new Intl.DateTimeFormat object based on params.
function createIntlDateTimeFormat(
args: LocalizeDateTimeParams,
): Intl.DateTimeFormat {
const options: Intl.DateTimeFormatOptions = {};
if (args.includeDate !== false) {
Expand Down
Loading