From 691acbc061f7675797f33310e78fb4d3b5588664 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 9 Feb 2025 18:25:52 +0100 Subject: [PATCH] optimize component --- src/lib/components/News.svelte | 54 +++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/lib/components/News.svelte b/src/lib/components/News.svelte index d1657355..48f85847 100644 --- a/src/lib/components/News.svelte +++ b/src/lib/components/News.svelte @@ -31,43 +31,51 @@ } } + // Memoize the YouTube video check function + const videoCheckCache = new Map(); function checkIfYoutubeVideo(link: string): string | null { + if (videoCheckCache.has(link)) { + return videoCheckCache.get(link); + } try { const url = new URL(link); - if ( - url.hostname === "www.youtube.com" || - url.hostname === "youtube.com" - ) { - return url.searchParams.get("v"); - } - return null; + const result = (url.hostname === "www.youtube.com" || url.hostname === "youtube.com") + ? url.searchParams.get("v") + : null; + videoCheckCache.set(link, result); + return result; } catch { + videoCheckCache.set(link, null); return null; } } + // Use a more efficient load more implementation function loadMoreData() { - const nextIndex = newsList.length; // Start from the current length of newsList - const remainingArticles = rawData?.slice(nextIndex, nextIndex + 20); // Get the next 20 articles - - // Append the new articles to the newsList - if (remainingArticles.length > 0) { - newsList = [...newsList, ...remainingArticles]; - } + const nextIndex = newsList.length; + newsList = [...newsList, ...rawData.slice(nextIndex, nextIndex + 20)]; } - $: filteredNewsList = - displaySection === "videos" - ? newsList.filter((item) => checkIfYoutubeVideo(item.url) !== null) - : displaySection === "press-releases" - ? rawDataPressRelease - : newsList; + // Pre-calculate video status once when data changes + $: hasVideos = rawData.some((item) => checkIfYoutubeVideo(item.url) !== null); + + // Optimize filtered list computation + $: filteredNewsList = (() => { + switch(displaySection) { + case 'videos': + return newsList.filter((item) => checkIfYoutubeVideo(item.url) !== null); + case 'press-releases': + return rawDataPressRelease; + default: + return newsList; + } + })(); $: { if ($stockTicker || $etfTicker) { rawData = data?.getNews || []; rawDataPressRelease = []; - newsList = rawData?.slice(0, 20) ?? []; + newsList = rawData?.slice(0, 10) ?? []; displaySection = "all"; } } @@ -109,7 +117,7 @@ : ''}">All - {#if rawData.some((item) => checkIfYoutubeVideo(item.url) !== null)} + {#if hasVideos}