commenthol / date-holidays

worldwide holidays
https://commenthol.github.io/date-holidays/
Other
906 stars 234 forks source link

performance issues #429

Closed Willham12 closed 9 months ago

Willham12 commented 9 months ago

In my case i have to check every day over a long period to check if its holiday. One of my tests running 200ms without the holiday check and over 12s with the holiday check.

Here is small example:

        const today = moment("2020-01-01T00:00");
        const hd = new Holidays();
        hd.init("DE", "B");

        for (let i = 0; i < 1000; i++) {
            const isHoliday = hd.isHoliday(today.toDate());
            if (isHoliday) {
                for (const h of isHoliday) {
                    if (h.type === "public") {
                        console.log(today.toLocaleString());
                    }
                }
            }
            today.add(1, "day");
        }
Willham12 commented 9 months ago

All my tests are running 15sec with moment-feiertage holiday check and over 1 day with this library.

commenthol commented 9 months ago

The library provides a list of holidays per gregorian year. So iterating over each day is not the best idea. This does the same in a matter of milliseconds.

const hd = new Holidays();
hd.init("DE", "B", { types: ['public']});

for (let y = 2020; y < 2023; y++) {
  const list = hd.getHolidays(y);
  for (const { date } of list) {
    console.log(date);
  }
}