Skip to content

Commit

Permalink
Speed up non-ISO calendar tests about 6x
Browse files Browse the repository at this point in the history
I was wrong about what was making non-ISO calendars so slow. I
thought the problem was `formatToParts()`, but it turns out that the
`DateTimeFormat` constructor is really slow and also allocates
ridiculous amounts of RAM. See more details here:
https://bugs.chromium.org/p/v8/issues/detail?id=6528

@littledan in https://bugs.chromium.org/p/v8/issues/detail?id=6528#c4
recommended to cache DateTimeFormat instances, so that's what this
commit does.

The result is a 6x speedup in non-ISO calendar tests.
Before: 6398.83ms
After: 1062.26ms

A similar speedup is likely for `ES.GetCanonicalTimeZoneIdentifier`.
Caching time zone canonicalization (in a separate PR) should have
a big positive impact on ZonedDateTIme and TimeZone perf.

Many thanks to @fer22f for uncovering this optimization in
js-temporal/temporal-polyfill#7.
  • Loading branch information
justingrant authored and ptomato committed Jul 9, 2021
1 parent 068a09c commit a012b4f
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CALENDAR_ID, ISO_YEAR, ISO_MONTH, ISO_DAY, CreateSlots, GetSlot, HasSlo

const ArrayIncludes = Array.prototype.includes;
const ArrayPrototypePush = Array.prototype.push;
const IntlDateTimeFormat = globalThis.Intl.DateTimeFormat;
const MathAbs = Math.abs;
const MathFloor = Math.floor;
const ObjectAssign = Object.assign;
Expand Down Expand Up @@ -430,19 +431,31 @@ function simpleDateDiff(one, two) {
*/
const nonIsoHelperBase = {
// The properties and methods below here should be the same for all lunar/lunisolar calendars.
getFormatter() {
// `new Intl.DateTimeFormat()` is amazingly slow and chews up RAM. Per
// https://bugs.chromium.org/p/v8/issues/detail?id=6528#c4, we cache one
// DateTimeFormat instance per calendar. Caching is lazy so we only pay for
// calendars that are used. Note that the nonIsoHelperBase object is spread
// into each each calendar's implementation before any cache is created, so
// each calendar gets its own separate cached formatter.
if (typeof this.formatter === 'undefined') {
this.formatter = new IntlDateTimeFormat(`en-US-u-ca-${this.id}`, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
era: this.eraLength,
timeZone: 'UTC'
});
}
return this.formatter;
},
isoToCalendarDate(isoDate, cache) {
let { year: isoYear, month: isoMonth, day: isoDay } = isoDate;
const key = JSON.stringify({ func: 'isoToCalendarDate', isoYear, isoMonth, isoDay, id: this.id });
const cached = cache.get(key);
if (cached) return cached;

const dateTimeFormat = new Intl.DateTimeFormat(`en-US-u-ca-${this.id}`, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
era: this.eraLength,
timeZone: 'UTC'
});
const dateTimeFormat = this.getFormatter();
let parts, isoString;
try {
isoString = toUtcIsoDateString({ isoYear, isoMonth, isoDay });
Expand Down Expand Up @@ -1604,14 +1617,7 @@ const helperChinese = ObjectAssign({}, nonIsoHelperBase, {
const key = JSON.stringify({ func: 'getMonthList', calendarYear, id: this.id });
const cached = cache.get(key);
if (cached) return cached;
const dateTimeFormat = new Intl.DateTimeFormat(`en-US-u-ca-${this.id}`, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
era: 'short',
timeZone: 'UTC'
});

const dateTimeFormat = this.getFormatter();
const getCalendarDate = (isoYear, daysPastFeb1) => {
const isoStringFeb1 = toUtcIsoDateString({ isoYear, isoMonth: 2, isoDay: 1 });
const legacyDate = new Date(isoStringFeb1);
Expand Down

0 comments on commit a012b4f

Please sign in to comment.