Skip to content

Commit

Permalink
Replace plain-object caches with Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Nov 23, 2023
1 parent 6e46e7f commit e11b46d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/DailyLearning.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @private */
const cals = Object.create(null);
const cals = new Map();

/**
* Plug-ins for daily learning calendars such as Daf Yomi, Mishna Yomi, Nach Yomi, etc.
Expand All @@ -16,7 +16,7 @@ export class DailyLearning {
if (typeof calendar !== 'function') {
throw new TypeError(`Invalid calendar function: ${calendar}`);
}
cals[name] = calendar;
cals.set(name, calendar);
}

/**
Expand All @@ -27,7 +27,7 @@ export class DailyLearning {
* @return {Event}
*/
static lookup(name, hd) {
const fn = cals[name];
const fn = cals.get(name);
if (typeof fn === 'function') {
return fn(hd);
}
Expand Down
10 changes: 5 additions & 5 deletions src/gematriya.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const heb2num = {
'ש': 300,
'ת': 400,
};
const num2heb = {};
const num2heb = new Map();
Object.keys(heb2num).forEach((key) => {
const val = heb2num[key];
num2heb[val] = key;
num2heb.set(val, key);
});

/**
Expand Down Expand Up @@ -81,19 +81,19 @@ export function gematriya(number) {
if (thousands > 0 && thousands !== 5) {
const tdigits = num2digits(thousands);
for (const tdig of tdigits) {
str += num2heb[tdig];
str += num2heb.get(tdig);
}
str += GERESH;
}
const digits = num2digits(num % 1000);
if (digits.length == 1) {
return str + num2heb[digits[0]] + GERESH;
return str + num2heb.get(digits[0]) + GERESH;
}
for (let i = 0; i < digits.length; i++) {
if (i + 1 === digits.length) {
str += GERSHAYIM;
}
str += num2heb[digits[i]];
str += num2heb.get(digits[i]);
}
return str;
}
Expand Down
6 changes: 3 additions & 3 deletions src/getTimezoneOffset.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const _formatters = {};
const _formatters = new Map();

/**
* @private
* @param {string} tzid
* @return {Intl.DateTimeFormat}
*/
function getFormatter(tzid) {
const fmt = _formatters[tzid];
const fmt = _formatters.get(tzid);
if (fmt) return fmt;
const f = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
Expand All @@ -18,7 +18,7 @@ function getFormatter(tzid) {
hour12: false,
timeZone: tzid,
});
_formatters[tzid] = f;
_formatters.set(tzid, f);
return f;
}

Expand Down
12 changes: 6 additions & 6 deletions src/hebcal.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const EREV = flags.EREV;
const CHOL_HAMOED = flags.CHOL_HAMOED;
const YOM_KIPPUR_KATAN = flags.YOM_KIPPUR_KATAN;

const unrecognizedAlreadyWarned = Object.create(null);
const unrecognizedAlreadyWarned = new Set();
const RECOGNIZED_OPTIONS = {
location: 1,
year: 1,
Expand Down Expand Up @@ -114,9 +114,9 @@ const RECOGNIZED_OPTIONS = {
*/
function warnUnrecognizedOptions(options) {
Object.keys(options).forEach((k) => {
if (typeof RECOGNIZED_OPTIONS[k] === 'undefined' && !unrecognizedAlreadyWarned[k]) {
if (typeof RECOGNIZED_OPTIONS[k] === 'undefined' && !unrecognizedAlreadyWarned.has(k)) {
console.warn(`Ignoring unrecognized HebrewCalendar option: ${k}`);
unrecognizedAlreadyWarned[k] = true;
unrecognizedAlreadyWarned.add(k);
}
});
}
Expand Down Expand Up @@ -489,7 +489,7 @@ function observedInDiaspora(ev) {
return ev.observedInDiaspora();
}

const yearArrayCache = Object.create(null);
const yearArrayCache = new Map();

/**
* HebrewCalendar is the main interface to the `@hebcal/core` library.
Expand Down Expand Up @@ -811,7 +811,7 @@ export class HebrewCalendar {
*/
static getHolidaysForYearArray(year, il) {
const cacheKey = `${year}-${il ? 1 : 0}`;
let events = yearArrayCache[cacheKey];
let events = yearArrayCache.get(cacheKey);
if (events) {
return events;
}
Expand All @@ -828,7 +828,7 @@ export class HebrewCalendar {
events = events.concat(filtered);
}
}
yearArrayCache[cacheKey] = events;
yearArrayCache.set(cacheKey, events);
return events;
}

Expand Down
6 changes: 3 additions & 3 deletions src/holidays.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export function getSedra_(hyear, il) {

const emojiIsraelFlag = {emoji: '🇮🇱'};
const chanukahEmoji = '🕎';
const yearCache = Object.create(null);
const yearCache = new Map();

const KEYCAP_DIGITS = [
'0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣',
Expand All @@ -345,7 +345,7 @@ export function getHolidaysForYear_(year) {
} else if (year < 1 || year > 32658) {
throw new RangeError(`Hebrew year ${year} out of range 1-32658`);
}
const cached = yearCache[year];
const cached = yearCache.get(year);
if (cached) {
return cached;
}
Expand Down Expand Up @@ -547,7 +547,7 @@ export function getHolidaysForYear_(year) {
add(new HolidayEvent(hd, 'Birkat Hachamah', MINOR_HOLIDAY, {emoji: '☀️'}));
}

yearCache[year] = h;
yearCache.set(year, h);
return h;
}

Expand Down
15 changes: 8 additions & 7 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const alias = {
};

/** @private */
const locales = Object.create(null);
const locales = new Map();
/** @private */
let activeLocale = null;
/** @private */
Expand All @@ -34,7 +34,7 @@ export class Locale {
*/
static lookupTranslation(id, locale) {
const locale0 = locale?.toLowerCase();
const loc = (typeof locale == 'string' && locales[locale0]) || activeLocale;
const loc = (typeof locale == 'string' && locales.get(locale0)) || activeLocale;
const array = loc[id];
if (array && array.length && array[0].length) {
return array[0];
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Locale {
if (typeof data.contexts !== 'object' || typeof data.contexts[''] !== 'object') {
throw new TypeError(`Locale '${locale}' invalid compact format`);
}
locales[locale.toLowerCase()] = data.contexts[''];
locales.set(locale.toLowerCase(), data.contexts['']);
}

/**
Expand All @@ -82,7 +82,7 @@ export class Locale {
throw new TypeError(`Invalid locale name: ${locale}`);
}
const locale0 = locale.toLowerCase();
const loc = locales[locale0];
const loc = locales.get(locale0);
if (!loc) {
throw new TypeError(`Unknown locale: ${locale}`);
}
Expand Down Expand Up @@ -110,7 +110,7 @@ export class Locale {
throw new TypeError(`Invalid locale name: ${locale}`);
}
const locale0 = locale.toLowerCase();
const loc = locales[locale0];
const loc = locales.get(locale0);
if (!loc) {
throw new TypeError(`Unknown locale: ${locale}`);
}
Expand All @@ -131,7 +131,7 @@ export class Locale {
*/
static useLocale(locale) {
const locale0 = locale.toLowerCase();
const obj = locales[locale0];
const obj = locales.get(locale0);
if (!obj) {
throw new RangeError(`Locale '${locale}' not found`);
}
Expand All @@ -153,7 +153,8 @@ export class Locale {
* @return {string[]}
*/
static getLocaleNames() {
return Object.keys(locales).sort((a, b) => a.localeCompare(b));
const keys = Array.from(locales.keys());
return keys.sort((a, b) => a.localeCompare(b));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/sedra.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class Sedra {
}
return new HDate(this.firstSaturday + (idx * 7));
} else if (typeof parsha === 'string') {
const num = parsha2id[parsha];
const num = parsha2id.get(parsha);
if (typeof num === 'number') {
return this.find(num);
} else if (parsha.indexOf('-') !== -1) {
Expand All @@ -169,8 +169,8 @@ export class Sedra {
typeof parsha[0] === 'string' && typeof parsha[1] === 'string') {
const p1 = parsha[0];
const p2 = parsha[1];
const num1 = parsha2id[p1];
const num2 = parsha2id[p2];
const num1 = parsha2id.get(p1);
const num2 = parsha2id.get(p2);
if (num2 === num1 + 1) {
return this.find(-num1);
} else {
Expand Down Expand Up @@ -297,10 +297,10 @@ export const parshiot = [
'Ha\'azinu',
];

const parsha2id = {};
const parsha2id = new Map();
for (let id = 0; id < parshiot.length; id++) {
const name = parshiot[id];
parsha2id[name] = id;
parsha2id.set(name, id);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/tachanun.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function range(start, end) {
return arr;
}

const cache = Object.create(null);
const cache = new Map();

const NONE = {
shacharit: false,
Expand Down Expand Up @@ -43,7 +43,11 @@ export function tachanun_(hdate, il) {
function tachanun0(hdate, il, checkNext) {
const year = hdate.getFullYear();
const key = `${year}-${il ? 1 : 0}`;
const dates = cache[key] = cache[key] || tachanunYear(year, il);
const cached = cache.get(key);
const dates = cached || tachanunYear(year, il);
if (!cached) {
cache.set(key, dates);
}
const abs = hdate.abs();
if (dates.none.indexOf(abs) > -1) {
return NONE;
Expand Down

0 comments on commit e11b46d

Please sign in to comment.