Skip to content

Commit

Permalink
Speed up non-ISO calendar perf about 10x
Browse files Browse the repository at this point in the history
  • Loading branch information
justingrant authored and ptomato committed Jul 9, 2021
1 parent 5132cc3 commit 559cb66
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions 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 559cb66

Please sign in to comment.