Skip to content

Commit

Permalink
Cache makeTorahMemoText() for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Nov 3, 2021
1 parent 98e27ca commit 63d5734
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/icalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,36 @@ export function eventToIcal(ev, options) {
return ical.toString();
}

const torahMemoCache = new Map();

const HOLIDAY_IGNORE_MASK = flags.DAF_YOMI | flags.OMER_COUNT |
flags.SHABBAT_MEVARCHIM | flags.MOLAD | flags.USER_EVENT |
flags.HEBREW_DATE;

/**
* @private
* @param {Event} ev
* @param {boolean} il
* @return {string}
*/
function makeTorahMemo(ev, il) {
if (ev.getFlags() & HOLIDAY_IGNORE_MASK) {
return '';
}
const hd = ev.getDate();
const yy = hd.getFullYear();
const mm = hd.getMonth();
const dd = hd.getDate();
const key = [yy, mm, dd, il ? '1' : '0', ev.getDesc()].join('-');
let memo = torahMemoCache.get(key);
if (typeof memo === 'string') {
return memo;
}
memo = makeTorahMemoText(ev, il).replace(/\n/g, '\\n');
torahMemoCache.set(key, memo);
return memo;
}

/**
* @private
* @param {Event} e
Expand All @@ -320,7 +350,7 @@ function createMemo(e, options) {
return e.getTodayIs('en') + '\\n\\n' + e.memo;
}
const url = appendTrackingToUrl(e.url(), options);
const torahMemo = makeTorahMemoText(e, options.il).replace(/\n/g, '\\n');
const torahMemo = makeTorahMemo(e, options.il);
if (mask & flags.PARSHA_HASHAVUA) {
return torahMemo + '\\n\\n' + url;
} else {
Expand Down

0 comments on commit 63d5734

Please sign in to comment.