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; } }