Skip to content

Commit

Permalink
Optimize findIndex for small scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Oct 13, 2024
1 parent 4fc5a45 commit 26d8e47
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ export const computeRange = (
// Clamp because prevStartIndex may exceed the limit when children decreased a lot after scrolling
prevStartIndex = min(prevStartIndex, cache._length - 1);

const shouldSearchForward =
computeOffset(cache, prevStartIndex) <= scrollOffset;
const start = findIndex(
cache,
scrollOffset,
shouldSearchForward ? prevStartIndex : undefined,
shouldSearchForward ? undefined : prevStartIndex
);
return [start, findIndex(cache, scrollOffset + viewportSize, start)];
if (computeOffset(cache, prevStartIndex) <= scrollOffset) {
// search forward
// start <= end, prevStartIndex <= start
const end = findIndex(cache, scrollOffset + viewportSize, prevStartIndex);
return [findIndex(cache, scrollOffset, prevStartIndex, end), end];
} else {
// search backward
// start <= end, start <= prevStartIndex
const start = findIndex(cache, scrollOffset, undefined, prevStartIndex);
return [start, findIndex(cache, scrollOffset + viewportSize, start)];
}
};

/**
Expand Down

0 comments on commit 26d8e47

Please sign in to comment.