mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-21 11:43:41 +08:00
refactor: optimize performance in various components; adjust animation intervals, memoize components, and improve sorting logic
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user