Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(core): plainDateFormat reuses one formatter per locale and options (#8335)

Christopher Chedeauae7ac1864c85
diff --git a/.changeset/plain-date-format-reuses-formatters.md b/.changeset/plain-date-format-reuses-formatters.md
new file mode 100644
index 0000000000..238d7346d3
--- /dev/null
+++ b/.changeset/plain-date-format-reuses-formatters.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[perf] Calendar, DateInput and the other date components now reuse one date formatter per locale and format instead of building one for every label they show, so rendering a month (42 day labels) no longer builds 42 formatters.
+
+@vjeux
diff --git a/packages/core/src/utils/plainDate.test.ts b/packages/core/src/utils/plainDate.test.ts
index ff8a37987e..abf48f7ddd 100644
--- a/packages/core/src/utils/plainDate.test.ts
+++ b/packages/core/src/utils/plainDate.test.ts
@@ -7,6 +7,7 @@ import {
plainDateFromISO,
plainDateToISO,
plainDateToDate,
+ dateTimeFormatFor,
plainDateFromDate,
plainDateToday,
getDaysInMonth,
@@ -28,6 +29,7 @@ import {
plainDateGetWeekNumber,
plainDateFormat,
formatSharedDate,
+ DATE_FORMAT_LONG,
DATE_FORMAT_WITH_WEEKDAY,
DATE_FORMAT_MONTH_YEAR,
plainDateWeekFieldName,
@@ -754,6 +756,94 @@ describe('plainDateGetWeekNumber', () => {
});
describe('plainDateFormat', () => {
+ it('keeps one formatter per locale and options, whatever order the options are in', () => {
+ // A month of day labels, as a calendar renders them, reuses one formatter.
+ const first = dateTimeFormatFor('en-NZ', DATE_FORMAT_WITH_WEEKDAY);
+ expect(dateTimeFormatFor('en-NZ', DATE_FORMAT_WITH_WEEKDAY)).toBe(first);
+ expect(
+ dateTimeFormatFor('en-NZ', {
+ year: 'numeric',
+ day: 'numeric',
+ month: 'long',
+ weekday: 'long',
+ }),
+ ).toBe(first);
+ // Other options, or another locale, get their own.
+ expect(dateTimeFormatFor('en-NZ', DATE_FORMAT_MONTH_YEAR)).not.toBe(first);
+ expect(dateTimeFormatFor('en-ZA', DATE_FORMAT_WITH_WEEKDAY)).not.toBe(
+ first,
+ );
+ });
+
+ it('formats the same date after the host zone moves', () => {
+ // The page stays open while the device travels east: a formatter cached
+ // in the first zone must not move the date.
+ const zone = process.env.TZ;
+ try {
+ process.env.TZ = 'America/Los_Angeles';
+ expect(
+ plainDateFormat(
+ {year: 2026, month: 2, day: 1},
+ DATE_FORMAT_MONTH_YEAR,
+ 'en-IE',
+ ),
+ ).toBe('February 2026');
+ process.env.TZ = 'America/New_York';
+ expect(
+ plainDateFormat(
+ {year: 2026, month: 2, day: 1},
+ DATE_FORMAT_MONTH_YEAR,
+ 'en-IE',
+ ),
+ ).toBe('February 2026');
+ process.env.TZ = 'Pacific/Kiritimati';
+ expect(
+ plainDateFormat(
+ {year: 2026, month: 2, day: 1},
+ DATE_FORMAT_WITH_WEEKDAY,
+ 'en-IE',
+ ),
+ ).toBe('Sunday 1 February 2026');
+ } finally {
+ if (zone === undefined) {
+ delete process.env.TZ;
+ } else {
+ process.env.TZ = zone;
+ }
+ }
+ });
+
+ it('prints a zone name or a time of day in the host zone, as before', () => {
+ // A PlainDate has neither: such fields read local midnight in the zone
+ // current at the call, not the UTC the date-only path formats in.
+ const zone = process.env.TZ;
+ try {
+ process.env.TZ = 'America/Los_Angeles';
+ const withZone = plainDateFormat(
+ {year: 2026, month: 1, day: 25},
+ {...DATE_FORMAT_LONG, timeZoneName: 'short'},
+ 'en-US',
+ );
+ expect(withZone).toContain('January 25, 2026');
+ expect(withZone).toContain('PST');
+ expect(withZone).not.toContain('UTC');
+ process.env.TZ = 'America/New_York';
+ expect(
+ plainDateFormat(
+ {year: 2026, month: 1, day: 25},
+ {...DATE_FORMAT_LONG, timeZoneName: 'short'},
+ 'en-US',
+ ),
+ ).toContain('EST');
+ } finally {
+ if (zone === undefined) {
+ delete process.env.TZ;
+ } else {
+ process.env.TZ = zone;
+ }
+ }
+ });
+
it('returns a human-readable date string with weekday', () => {
const result = plainDateFormat(
{year: 2026, month: 1, day: 25},
diff --git a/packages/core/src/utils/plainDate.ts b/packages/core/src/utils/plainDate.ts
index e956c68788..be69bcf984 100644
--- a/packages/core/src/utils/plainDate.ts
+++ b/packages/core/src/utils/plainDate.ts
@@ -374,10 +374,65 @@ export function plainDateFormat(
options: Intl.DateTimeFormatOptions,
locale: Locale = 'en',
): string {
- return new Intl.DateTimeFormat(locale, {
+ const calendar = options.calendar ?? 'gregory';
+ if (options.timeZone != null) {
+ return dateTimeFormatFor(locale, {...options, calendar}).format(
+ plainDateToDate(pd),
+ );
+ }
+ if (TIME_OF_DAY_FIELDS.some(field => options[field] != null)) {
+ // A zone name or a time of day reads local midnight in the zone current
+ // at the call. A cached formatter would keep the zone it was built in,
+ // so this one is built for the call.
+ return new Intl.DateTimeFormat(locale, {...options, calendar}).format(
+ plainDateToDate(pd),
+ );
+ }
+ // Date fields alone: UTC midnight formatted in UTC, so neither the cached
+ // formatter nor the date depends on the host's zone, which can change while
+ // a page is open.
+ return dateTimeFormatFor(locale, {
...options,
- calendar: options.calendar ?? 'gregory',
- }).format(plainDateToDate(pd));
+ calendar,
+ timeZone: 'UTC',
+ }).format(utcTime(pd.year, pd.month - 1, pd.day));
+}
+
+/** Options that print a time of day or a zone, which a PlainDate lacks. */
+const TIME_OF_DAY_FIELDS = [
+ 'timeZoneName',
+ 'hour',
+ 'minute',
+ 'second',
+ 'fractionalSecondDigits',
+ 'dayPeriod',
+ 'timeStyle',
+] as const satisfies ReadonlyArray<keyof Intl.DateTimeFormatOptions>;
+
+/**
+ * One `Intl.DateTimeFormat` per locale and options, built on first use.
+ * Building a formatter costs far more than formatting with it, and a calendar
+ * formats each of a month's 42 day labels with the same options on every
+ * render. The options are keyed in a fixed order, so the same options written
+ * in another order share a formatter.
+ */
+const dateTimeFormats = new Map<string, Intl.DateTimeFormat>();
+
+/** @internal plainDateFormat's formatter cache; not part of the public API. */
+export function dateTimeFormatFor(
+ locale: Locale,
+ options: Intl.DateTimeFormatOptions,
+): Intl.DateTimeFormat {
+ const key = JSON.stringify([
+ locale,
+ Object.entries(options).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
+ ]);
+ let format = dateTimeFormats.get(key);
+ if (format == null) {
+ format = new Intl.DateTimeFormat(locale, options);
+ dateTimeFormats.set(key, format);
+ }
+ return format;
}
/**