Sub topic 7
Overscan: avoiding blank gaps while scrolling
This chapter assumes you have read the previous chapters. You should know how the visible range is computed from scrollTop and row height, and how the spacer and absolutely positioned rows work.
Why blank gaps appear
A virtual list renders only the rows that intersect the viewport. When the user scrolls, the browser moves the content immediately and paints it on its own schedule. React then has to receive the scroll event, update state, render and commit the new rows. Until that finishes, the browser can paint a frame in which the viewport shows empty spacer space where rows should be.
Fast scrolling, a slow device, or expensive row components make this worse. You see it as white flashes at the edge of the list.
What overscan does
Overscan renders a few extra rows above and below the visible range. These rows are already in the DOM before they scroll into view. A small scroll then needs no new rows, and a large one only needs to catch up on the rows beyond the buffer.
Adding overscan to the range
Overscan widens the range by a fixed number of rows on each side. Clamp the result so you never ask for a row that does not exist.
type Range = { first: number; last: number };
export function getRange(
scrollTop: number,
height: number,
rowHeight: number,
count: number,
overscan: number
): Range {
const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
const last = Math.min(
count - 1,
Math.ceil((scrollTop + height) / rowHeight) + overscan
);
return { first, last };
}
console.log(getRange(0, 400, 40, 1000, 3));
console.log(getRange(800, 400, 40, 1000, 3));The first call prints { first: 0, last: 13 }. The lower bound is clamped to 0, so there are no rows above to add. The second call prints { first: 17, last: 33 }. The visible rows are 20 to 30, and three extra rows are added on each side.
The Math.max and Math.min calls matter. Without them, you can get negative indexes near the top and undefined items near the bottom.
Choosing a value
Overscan trades render cost against scroll smoothness.
| Overscan | Effect |
|---|---|
| 0 | Fewest DOM nodes, but gaps appear on any quick scroll. |
| 3 to 5 | A good default for simple rows. |
| 10 or more | Fewer gaps with heavy rows, but more work per scroll update. |
Choose by measuring on a slow device, not on your development machine. Rows with images, charts, or many child components need more overscan than plain text rows. They also cost more to render, so a large value can slow every scroll update. If that happens, make the rows cheaper first and raise overscan second.
TanStack Virtual exposes the same setting as the overscan option of useVirtualizer, which the earlier example set to 5. The meaning is the same: extra items on each side of the visible range.
Common errors
- Overscan counted in pixels. The value is a number of rows, not a pixel distance. With 40px rows,
overscan: 5buffers 200px on each side. - Missing clamps. Unclamped ranges cause
items[i]to beundefinedat the ends of the list. - Treating overscan as a fix for slow rows. It hides the delay in rendering. It does not shorten it.
- Huge overscan to remove all gaps. At some point you are rendering most of the list again, which defeats virtualization.
Exercise
A list has 500 rows of 50px each, and the viewport is 300px tall. The user is at scrollTop = 1000 and overscan is 4. Which rows are rendered, using the range formula from this chapter?
Show answer
The first visible row is floor(1000 / 50) = 20, so first = 20 - 4 = 16. The last is ceil((1000 + 300) / 50) = 26, so last = 26 + 4 = 30. Rows 16 through 30 are rendered. Neither bound is clamped, because 16 is above 0 and 30 is below 499.