refactor: optimize performance in various components; adjust animation intervals, memoize components, and improve sorting logic

This commit is contained in:
kuekhaoyang
2025-11-18 17:43:09 +08:00
parent 79515e2702
commit 0c9844d9ce
9 changed files with 213 additions and 107 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ export function useInfiniteScroll({
onLoadMore(nextPage);
}
},
{ threshold: 0.1, rootMargin: '400px' }
{ threshold: 0.1, rootMargin: '200px' } // Reduced from 400px to 200px
);
prefetchObserver.observe(prefetchRef.current);
+23 -4
View File
@@ -116,11 +116,30 @@ export function useParallelSearch(
console.log(`[useParallelSearch] Received ${newVideos.length} videos from source ${data.source}`);
// Add videos and sort by relevance
// Optimized: Insert new videos in sorted position instead of re-sorting entire array
setResults((prev) => {
const combined = [...prev, ...newVideos];
// Sort by relevance score (highest first)
return combined.sort((a, b) => (b.relevanceScore || 0) - (a.relevanceScore || 0));
if (prev.length === 0) return newVideos;
// Binary insert for better performance
const combined = [...prev];
for (const video of newVideos) {
const score = video.relevanceScore || 0;
let insertIndex = combined.length;
// Find insert position using binary search
let left = 0;
let right = combined.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if ((combined[mid].relevanceScore || 0) >= score) {
left = mid + 1;
} else {
right = mid;
}
}
combined.splice(left, 0, video);
}
return combined;
});
// Update source stats