From da3ab81ae6708ad8e69619ea4fa8b0fe197af4d1 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Wed, 11 Feb 2026 22:56:00 -0800 Subject: [PATCH] perf(player): batch addAll/playAllNext to avoid cascading reactivity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit addAll() now collects all new tracks and pushes them in a single array operation instead of calling add() in a loop, which triggered per-item reactive updates and O(n²) existence checks. --- src/lib/player/store.svelte.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/player/store.svelte.ts b/src/lib/player/store.svelte.ts index 007af1d..592c374 100644 --- a/src/lib/player/store.svelte.ts +++ b/src/lib/player/store.svelte.ts @@ -169,8 +169,26 @@ class PlayerStore { } addAll(tracks: Track[]) { + // Batch: collect new tracks, push all at once + const newTracks: Track[] = []; for (const track of tracks) { - this.add(track); + // Check existence inline to avoid O(n) per-track via add() + if (!this.queue.some((t) => t.id === track.id)) { + newTracks.push(track); + } + } + if (newTracks.length === 0) return; + + const startIdx = this.queue.length; + this.queue.push(...newTracks); + + if (this.isShuffled) { + const newIndices = newTracks.map((_, i) => startIdx + i); + this.shuffledIndices.push(...newIndices); + } + + if (startIdx === 0 && !this.currentId) { + this.currentId = newTracks[0].id; } }