From 0bd6790b1fa734b3a45954d3a97329a2818aa4b4 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 14 Jun 2026 12:19:40 +0200 Subject: [PATCH] refactor(timeline): count timelineMeta totals in a single pass Replace the flatMap intermediate array plus two filter passes with one walk over the year bands and the undated bucket. Same counts, no throwaway allocation. Co-Authored-By: Claude Opus 4.8 --- frontend/src/lib/timeline/timelineMeta.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/frontend/src/lib/timeline/timelineMeta.ts b/frontend/src/lib/timeline/timelineMeta.ts index 7052c405..38b40ea7 100644 --- a/frontend/src/lib/timeline/timelineMeta.ts +++ b/frontend/src/lib/timeline/timelineMeta.ts @@ -21,11 +21,18 @@ export interface TimelineMeta { */ export function timelineMeta(timeline: TimelineDTO): TimelineMeta { const years = timeline.years; - const allEntries = [...years.flatMap((y) => y.entries), ...timeline.undated]; + let letterCount = 0; + let eventCount = 0; + const tally = (e: TimelineDTO['undated'][number]) => { + if (e.kind === 'LETTER') letterCount += 1; + else if (e.kind === 'EVENT') eventCount += 1; + }; + for (const y of years) for (const e of y.entries) tally(e); + for (const e of timeline.undated) tally(e); return { firstYear: years.length ? years[0].year : null, lastYear: years.length ? years[years.length - 1].year : null, - letterCount: allEntries.filter((e) => e.kind === 'LETTER').length, - eventCount: allEntries.filter((e) => e.kind === 'EVENT').length + letterCount, + eventCount }; }