From e1495d578ff5dd10322e6c4d3dc45a3418da645c Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 23 Jan 2026 00:15:58 -0600 Subject: [PATCH] perf(AlbumCard): remove per-card ResizeObserver for large libraries Previously each AlbumCard created a ResizeObserver on mount to detect text overflow for the ticker/marquee effect. With 10,000 cards, this meant 10,000+ active observers causing significant overhead. Changes: - Remove ResizeObserver creation in onMount - Measure overflow only on first mouseenter/focus event - Reset measurement state when title/artist props change - Ticker effect still works but only activates when user hovers This significantly reduces CPU and memory overhead for large album grids while preserving the visual effect when users interact with cards. Part of #13 - LocalLibrary performance optimization --- src/lib/components/AlbumCard.svelte | 37 +++++++++++++++-------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/lib/components/AlbumCard.svelte b/src/lib/components/AlbumCard.svelte index 3e8897add..2496a2de4 100644 --- a/src/lib/components/AlbumCard.svelte +++ b/src/lib/components/AlbumCard.svelte @@ -119,15 +119,19 @@ } } + // Track if overflow has been measured (only measure on first hover for performance) + let overflowMeasured = false; + + function measureOverflowOnce() { + if (!overflowMeasured) { + updateOverflow(); + overflowMeasured = true; + } + } + onMount(() => { - updateOverflow(); - const observer = new ResizeObserver(() => updateOverflow()); - if (titleRef) { - observer.observe(titleRef); - } - if (artistRef) { - observer.observe(artistRef); - } + // Don't create ResizeObserver per-card for performance in large libraries + // Overflow is measured on first hover instead if (albumId) { void loadAlbumFavorites(); @@ -135,22 +139,17 @@ const unsubscribe = subscribeAlbumFavorites(() => { favoriteFromStore = isAlbumFavorite(albumId); }); - return () => { - observer.disconnect(); - unsubscribe(); - }; + return () => unsubscribe(); } - return () => observer.disconnect(); }); + // Reset measurement when title/artist changes $effect(() => { title; - tick().then(updateOverflow); - }); - - $effect(() => { artist; - tick().then(updateOverflow); + overflowMeasured = false; + titleOverflow = 0; + artistOverflow = 0; }); @@ -159,6 +158,8 @@ style="width: {cardSize}px" data-search-id={searchId} onclick={handleCardClick} + onmouseenter={measureOverflowOnce} + onfocus={measureOverflowOnce} role="button" tabindex="0" onkeydown={(e) => e.key === 'Enter' && onclick?.()}