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
This commit is contained in:
vicrodh committed 2026-01-23 00:15:58 -06:00
1 parent da653d48fc
commit e1495d578f
1 file changed
+19 -18
+19 -18
View File
@@ -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;
});
</script>
@@ -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?.()}