Skip to content

Commit

Permalink
Remove some caches to reduce memory footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Dec 19, 2023
1 parent d44069f commit f4c8d84
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 40 deletions.
1 change: 1 addition & 0 deletions hebcal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ declare module '@hebcal/core' {
*/
find(parsha: number | string | string[]): HDate;
getYear(): number;
getSedraArray(): any[];
/**
* R.D. date of the first Saturday on or after Rosh Hashana
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/core",
"version": "5.0.3",
"version": "5.0.4",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"contributors": [
"Eyal Schachter (https://github.com/Scimonster)",
Expand Down
21 changes: 3 additions & 18 deletions src/hebcal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import {
makeWeekdayChanukahCandleLighting,
} from './candles.js';
import {flags} from './event.js';
import {getSedra_} from './sedra.js';
import {hallel_} from './hallel.js';
import {HDate} from './hdate.js';
import {HolidayEvent, getHolidaysForYear_, getSedra_} from './holidays.js';
import {HolidayEvent, getHolidaysForYear_} from './holidays.js';
import './locale-ashkenazi.js';
import './locale-he.js';
import {Locale} from './locale.js';
Expand Down Expand Up @@ -484,9 +485,6 @@ function observedInDiaspora(ev) {
return ev.observedInDiaspora();
}

const yearArrayCache = new Map();
const holidaysOnDate = new Map();

/**
* HebrewCalendar is the main interface to the `@hebcal/core` library.
* This namespace is used to calculate holidays, rosh chodesh, candle lighting & havdalah times,
Expand Down Expand Up @@ -811,15 +809,10 @@ export class HebrewCalendar {
* @return {Event[]}
*/
static getHolidaysForYearArray(year, il) {
const cacheKey = `${year}-${il ? 1 : 0}`;
let events = yearArrayCache.get(cacheKey);
if (events) {
return events;
}
const yearMap = getHolidaysForYear_(year);
const startAbs = HDate.hebrew2abs(year, TISHREI, 1);
const endAbs = HDate.hebrew2abs(year + 1, TISHREI, 1) - 1;
events = [];
let events = [];
const myFilter = il ? observedInIsrael : observedInDiaspora;
for (let absDt = startAbs; absDt <= endAbs; absDt++) {
const hd = new HDate(absDt);
Expand All @@ -829,7 +822,6 @@ export class HebrewCalendar {
events = events.concat(filtered);
}
}
yearArrayCache.set(cacheKey, events);
return events;
}

Expand All @@ -842,21 +834,14 @@ export class HebrewCalendar {
static getHolidaysOnDate(date, il) {
const hd = HDate.isHDate(date) ? date : new HDate(date);
const hdStr = hd.toString();
const cacheKey = hdStr + '/' +
(typeof il === 'undefined' ? 2 : il ? 1 : 0);
if (holidaysOnDate.has(cacheKey)) {
return holidaysOnDate.get(cacheKey);
}
const yearMap = getHolidaysForYear_(hd.getFullYear());
const events = yearMap.get(hdStr);
// if il isn't a boolean return both diaspora + IL for day
if (typeof il === 'undefined' || typeof events === 'undefined') {
holidaysOnDate.set(cacheKey, events);
return events;
}
const myFilter = il ? observedInIsrael : observedInDiaspora;
const filtered = events.filter(myFilter);
holidaysOnDate.set(cacheKey, filtered);
return filtered;
}

Expand Down
20 changes: 1 addition & 19 deletions src/holidays.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {Event, flags} from './event.js';
import {HDate} from './hdate.js';
import {Locale} from './locale.js';
import {dateYomHaShoah, dateYomHaZikaron} from './modern.js';
import {getSedra_} from './sedra.js';
import {MoladEvent} from './molad.js';
import {Sedra} from './sedra.js';
import {staticHolidays, staticModernHolidays} from './staticHolidays.js';

const CHAG = flags.CHAG;
Expand Down Expand Up @@ -261,24 +261,6 @@ const TEVET = months.TEVET;
const ADAR_I = months.ADAR_I;
const ADAR_II = months.ADAR_II;

const sedraCache = new Map();

/**
* @private
* @param {number} hyear
* @param {boolean} il
* @return {Sedra}
*/
export function getSedra_(hyear, il) {
const cacheKey = `${hyear}-${il ? 1 : 0}`;
let sedra = sedraCache.get(cacheKey);
if (!sedra) {
sedra = new Sedra(hyear, il);
sedraCache.set(cacheKey, sedra);
}
return sedra;
}

const emojiIsraelFlag = {emoji: '🇮🇱'};
const chanukahEmoji = '🕎';
const yearCache = new Map();
Expand Down
20 changes: 18 additions & 2 deletions src/sedra.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ export class Sedra {
}

/**
* @private
* @return {Object[]}
*/
getSedraArray() {
Expand Down Expand Up @@ -220,7 +219,7 @@ export class Sedra {
const index = this.theSedraArray[weekNum];

if (typeof index === 'undefined') {
const sedra = new Sedra(this.year + 1, this.il);
const sedra = getSedra_(this.year + 1, this.il);
return sedra.lookup(saturday); // must be next year
}
if (typeof index === 'string') {
Expand Down Expand Up @@ -495,3 +494,20 @@ types['1311'] = types['1221'];
* Kislev each have 30 days), and has Passover start on Thursday. */
types['1721'] = types['170'];

export const sedraCache = new Map();

/**
* @private
* @param {number} hyear
* @param {boolean} il
* @return {Sedra}
*/
export function getSedra_(hyear, il) {
const cacheKey = `${hyear}-${il ? 1 : 0}`;
let sedra = sedraCache.get(cacheKey);
if (!sedra) {
sedra = new Sedra(hyear, il);
sedraCache.set(cacheKey, sedra);
}
return sedra;
}

0 comments on commit f4c8d84

Please sign in to comment.