diff --git a/src/helpers/cinema.helper.ts b/src/helpers/cinema.helper.ts index d369863d..b3b9f7d8 100644 --- a/src/helpers/cinema.helper.ts +++ b/src/helpers/cinema.helper.ts @@ -52,14 +52,16 @@ export const parseCinema = (el: HTMLElement | null): { city: string; name: strin export const getGroupedFilmsByDate = (el: HTMLElement | null): CSFDCinemaGroupedFilmsByDate[] => { const divs = el.querySelectorAll(':scope > div'); - const getDatesAndFilms = divs - .map((_, index) => index) - .filter((index) => index % 2 === 0) - .map((index) => { - const [date, films] = divs.slice(index, index + 2); - const dateText = date?.firstChild?.textContent?.trim() ?? null; - return { date: dateText, films: getCinemaFilms('', films) }; - }); + const getDatesAndFilms: CSFDCinemaGroupedFilmsByDate[] = []; + + // Performance optimization: Avoid intermediate array allocations (.map().filter().map()) + // by using a single standard `for` loop with a step of 2. + for (let i = 0; i < divs.length; i += 2) { + const date = divs[i]; + const films = divs[i + 1]; + const dateText = date?.firstChild?.textContent?.trim() ?? null; + getDatesAndFilms.push({ date: dateText, films: getCinemaFilms('', films) }); + } return getDatesAndFilms; };