From 150d4a0f4612a3b3c562814d79e9e401fe6156c0 Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Fri, 4 Sep 2026 00:30:18 +0530 Subject: [PATCH] Optimize calculateFreebuffStreak to single-pass iteration The function iterated through usageDates twice: 1. Once with filter() to build the usageDateSet 2. Once with reduce() to find lastUsageDate This is O(2n) when it could be O(n) by combining both operations in a single loop. Combined both operations into one loop, building the set and tracking the latest date simultaneously. This is more efficient and clearer in intent. --- common/src/util/freebuff-streak.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/common/src/util/freebuff-streak.ts b/common/src/util/freebuff-streak.ts index f61f22832a..5db8980627 100644 --- a/common/src/util/freebuff-streak.ts +++ b/common/src/util/freebuff-streak.ts @@ -64,13 +64,15 @@ export function calculateFreebuffStreak(params: { lastUsageDate: string | null } { const { usageDates, todayDateKey } = params - const usageDateSet = new Set( - usageDates.filter((date) => date <= todayDateKey), - ) - const lastUsageDate = usageDates.reduce((latest, date) => { - if (date > todayDateKey) return latest - return latest === null || date > latest ? date : latest - }, null) + const usageDateSet = new Set() + let lastUsageDate: string | null = null + for (const date of usageDates) { + if (date > todayDateKey) continue + usageDateSet.add(date) + if (lastUsageDate === null || date > lastUsageDate) { + lastUsageDate = date + } + } const todayUsed = usageDateSet.has(todayDateKey) let anchorDateKey = todayDateKey