Skip to content

Commit 8a85ce2

Browse files
committed
Fix inefficient duplicate iteration in calculateFreebuffStreak
The calculateFreebuffStreak function was iterating 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. The fix combines both operations into one loop, building the set and tracking the latest date simultaneously. This is more efficient and clearer in intent.
1 parent 0444c4c commit 8a85ce2

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

common/src/util/freebuff-streak.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ export function calculateFreebuffStreak(params: {
6464
lastUsageDate: string | null
6565
} {
6666
const { usageDates, todayDateKey } = params
67-
const usageDateSet = new Set(
68-
usageDates.filter((date) => date <= todayDateKey),
69-
)
70-
const lastUsageDate = usageDates.reduce<string | null>((latest, date) => {
71-
if (date > todayDateKey) return latest
72-
return latest === null || date > latest ? date : latest
73-
}, null)
67+
const usageDateSet = new Set<string>()
68+
let lastUsageDate: string | null = null
69+
for (const date of usageDates) {
70+
if (date > todayDateKey) continue
71+
usageDateSet.add(date)
72+
if (lastUsageDate === null || date > lastUsageDate) {
73+
lastUsageDate = date
74+
}
75+
}
7476
const todayUsed = usageDateSet.has(todayDateKey)
7577

7678
let anchorDateKey = todayDateKey

0 commit comments

Comments
 (0)