Virtualized List
1. What is virtualization?
List virtualization (windowing) is a technique that renders only the items currently visible in a scrollable container, instead of the entire list. The DOM size stays constant regardless of how many items the data contains.
An empty spacer as tall as the full list gives the scrollbar its real size. The visible rows are rendered inside it and shifted into place.
DOM cost depends on the viewport, not the data.
<div className="viewport" onScroll={onScroll}>
{/* spacer: gives the scrollbar the full height */}
<div style={{ height: count * rowHeight }}>
<div style={{ transform: `translateY(${offsetY}px)` }}>
{rows.slice(start, end).map(renderRow)}
</div>
</div>
</div>
2. See it happen
The list has 10,000 rows. Watch the stats while you scroll.
- Rows in DOM: stays around 15 no matter where you are.
- Rendered vs Visible: the extra rows are overscan.
- Hatched rows: overscan rows just outside the viewport.
Try:
- Scroll fast to the bottom. The DOM count doesn't change.
- Set overscan to 0 and fling. You may see blank gaps.
- Inspect the list in DevTools: only a handful of row elements exist.
- Rows in data
- 10,000
- Rows in DOM
- 12
- scrollTop
- 0px
- Visible
- 0 - 8
- Rendered
- 0 - 11
- translateY
- 0px
- Spacer height
- 360,000px
Scroll fast: the DOM row count never moves. Set overscan to 0 and fling the list to see blank gaps before React catches up.
3. The math
With a fixed row height, everything is division:
- First visible row:
floor(scrollTop / rowHeight) - Rows that fit:
ceil(viewport / rowHeight) - Overscan pads both sides so rows exist before they scroll in.
The rendered block moves with translateY, not per-row top: one transform is cheaper than repositioning every row.
export type Range = { start: number; end: number; offsetY: number; totalHeight: number };
// Pure windowing math: which rows intersect the viewport, plus overscan on both sides.
export function getRange(
scrollTop: number,
viewportHeight: number,
rowHeight: number,
count: number,
overscan = 5,
): Range {
const first = Math.floor(scrollTop / rowHeight);
const visible = Math.ceil(viewportHeight / rowHeight);
const start = Math.max(0, first - overscan);
const end = Math.min(count, first + visible + overscan);
return { start, end, offsetY: start * rowHeight, totalHeight: count * rowHeight };
}
4. Try the code
virtual.ts is the pure math, useVirtualList.ts wires it to scroll, and App.tsx renders. Try changing ROW or the overscan.
5. Where it gets hard
- Variable row heights. The division trick dies. You need measured heights, a prefix sum of offsets, and a binary search for the first row; unmeasured rows use an estimate, so the scrollbar drifts as rows get measured. That's most of what TanStack Virtual does.
- Scroll jank on fling. Scroll is handled off the main thread, rendering isn't. If the fling outruns React, you see blank space. Overscan buys time; heavy rows lose it.
- Browser features disappear. Ctrl+F, anchor links, and screen-reader "list of 10,000 items" only see rendered rows. Set
aria-rowcount/aria-setsizeandaria-rowindex/aria-posinsetso assistive tech knows the real size. - Huge lists hit a height ceiling. Browsers cap how tall an element can be (tens of millions of pixels, engine-specific). Past that, the spacer is clamped and the bottom rows become unreachable.
6. Common mistakes
| Looks like | Actually | Why |
|---|---|---|
key={index} is fine here | State leaks between rows on insert/delete | Index keys follow the slot, not the item; use the item id |
| Row inputs keep their value | Lost when scrolled away | Unmounted rows lose local state; lift it into data |
| Fewer DOM nodes = always faster | Not for 200 simple rows | Scroll handler + re-render can cost more than static DOM |
onScroll runs once per frame | Can run more often | Batch to one state update per frame with requestAnimationFrame if rows are heavy |
For mid-sized lists, try CSS first: content-visibility: auto skips layout and paint for off-screen elements while keeping them in the DOM, so Ctrl+F and accessibility still work.
Interview answer
List virtualization is a technique that renders only the items visible in a scrollable container rather than the whole list, so DOM size and rendering cost stay constant regardless of data size. A spacer element sized to the full list preserves the scrollbar, and on scroll the visible range is computed from the scroll offset and item height, with a few extra items rendered as overscan to avoid blank gaps. Fixed heights make the math simple; variable heights require measuring items and estimating offsets. The trade-offs are lost native behavior such as find-in-page and extra accessibility work, so it is worth it only for genuinely large lists.
Resources
- TanStack Virtual: headless virtualizer; read its measuring logic for variable heights.
- Virtualize large lists with react-window: web.dev walkthrough.
- MDN: content-visibility: the no-JS alternative.