Sub topic 10
Rendering only what is visible: list virtualization
This chapter assumes you can read a Profiler flamegraph, know the difference between a render and a commit, and have used React.memo. Memoization makes each row cheaper to skip. Virtualization removes most rows from the tree.
The problem: thousands of rows
A list of 10,000 items creates 10,000 components and 10,000 DOM nodes. A profile of this page shows two symptoms:
- The mount commit is long, because every row renders once.
- Every update that touches the list parent re-renders every row, or at least runs the memo comparison on every row.
Memoization reduces the second cost but not the first. The browser also pays for layout and paint of nodes the user cannot see. If the flamegraph shows a wide bar made of thousands of thin, similar children, virtualization is a likely fix.
How virtualization works
A virtualized list (also called windowing) renders only the rows inside the scroll viewport, plus a few extra rows above and below as a buffer. The list container keeps its full height, so the scrollbar behaves as if all rows existed. As the user scrolls, the library changes which rows are mounted and positions them with absolute offsets.
A fixed-height example
For fixed row heights, the offset math is simple enough to write by hand. This example renders a window of rows from a large array.
import { useState } from "react";
const ROW_HEIGHT = 32;
const VIEWPORT_HEIGHT = 320;
const OVERSCAN = 3;
export function VirtualList({ items }) {
const [scrollTop, setScrollTop] = useState(0);
const first = Math.max(0, Math.floor(scrollTop / ROW_HEIGHT) - OVERSCAN);
const last = Math.min(
items.length - 1,
Math.ceil((scrollTop + VIEWPORT_HEIGHT) / ROW_HEIGHT) + OVERSCAN
);
const rows = [];
for (let i = first; i <= last; i++) {
rows.push(
<div
key={items[i].id}
style={{
position: "absolute",
top: i * ROW_HEIGHT,
height: ROW_HEIGHT,
left: 0,
right: 0,
}}
>
{items[i].name}
</div>
);
}
return (
<div
style={{ height: VIEWPORT_HEIGHT, overflowY: "auto" }}
onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}
>
<div style={{ height: items.length * ROW_HEIGHT, position: "relative" }}>
{rows}
</div>
</div>
);
}The inner div has the full height, so the scrollbar is correct. With 10,000 items, about 16 rows exist at a time.
Using a library
In real projects, use a maintained library instead of writing this yourself. Common choices are react-window, @tanstack/react-virtual, and react-virtuoso. They handle variable row heights, horizontal lists, grids, and scroll restoration. Check each library's current documentation for its API, since props and exports differ between major versions.
Gotchas
- Variable heights. Rows whose height depends on content need measurement. This is harder and can cause scroll jumps if heights are guessed wrong.
- Browser find and accessibility. Unmounted rows are not in the DOM, so Ctrl+F cannot find them and screen readers do not see them. Consider this before virtualizing short lists.
- Row state. A row that scrolls out of view unmounts and loses its local state. Keep state such as "expanded" in the parent or in data.
- Stable keys. Use item ids, not indexes, so rows keep identity as the window moves.
- Scroll re-renders. In the example, every scroll event re-renders the list component. This is cheap because only about 16 rows exist, but memoized rows still help when each row is costly.
Verify the change
Record the same interaction before and after. In the Profiler, compare the mount commit duration and the number of components in the flamegraph. Take the measurement in a production build, as covered earlier, since development builds inflate the numbers.
Exercise
A list of 5,000 rows uses React.memo on each row, yet the first mount commit is still very slow. Why does memoization not fix this, and what does virtualization change?
Show answer
React.memo only lets React skip re-rendering a row when its props are unchanged. On first mount every row must render, so memo cannot help. Virtualization renders only the rows near the viewport, so the mount creates a few dozen components and DOM nodes instead of 5,000.