Sub topic 3
The windowing idea: render only what is visible
This chapter assumes you know React function components, useState, and basic CSS positioning and scroll behavior. It introduces the core idea behind list virtualization. The next chapters turn it into code.
The problem in one sentence
A list of 10,000 rows costs React and the browser 10,000 rows of work, but a screen shows about 20 of them. The other 9,980 rows are created, laid out, and painted for nobody.
The idea
Windowing (also called virtualization) renders only the rows that fall inside a "window" over the list: the part currently visible in the scroll container, plus a few rows of margin. As the user scrolls, the window moves. Rows that leave it are unmounted, and rows that enter it are mounted.
The data stays complete. All 10,000 items still live in an array in memory. Only the DOM is limited to a small slice.
// Naive: the DOM gets one node per item
items.map((item) => <Row key={item.id} item={item} />);
// Windowed: the DOM gets one node per visible item
items.slice(first, last + 1).map((item) => <Row key={item.id} item={item} />);The second version is the whole idea in one line. The rest of the technique is about computing first and last, and about making the scrollbar behave as if the other rows were there.
What has to stay true
Dropping rows from the DOM creates a problem. A scroll container gets its scrollbar size from its content. If only 25 rows exist, the scrollbar would be tiny and the scroll range would be wrong. A virtual list therefore has to keep three things consistent:
- Total height. The scrollable area must be as tall as the full list would be, so the scrollbar thumb has the right size and range.
- Row position. Each rendered row must appear at the offset it would have had in the full list. Row 5,000 must show up at the right scroll position, not at the top.
- Window position. The set of rendered rows must follow the scroll position, updating as the user scrolls.
The next two chapters cover how to satisfy the first two with a spacer element and absolute positioning, and how to compute the third from scrollTop.
What you gain and what you give up
Windowing makes the cost of a list depend on the size of the screen instead of the size of the data. Mount time, memory use, and the cost of each re-render stay roughly flat whether the list has 1,000 rows or 1,000,000.
It also removes things the browser gives you for free with a plain list:
| Behavior | Plain list | Windowed list |
|---|---|---|
| Find-in-page (Ctrl+F) matches every row | Yes | Only rendered rows |
| Row state survives scrolling away | Yes | No, unless you store it outside the row |
| Scrollbar size from content | Automatic | You must supply the total height |
| Row height known by the browser | Yes | You must know or measure it |
Where the window comes from
The window is not an abstract concept in the code. It is two integers, a first index and a last index, derived from three inputs: the scroll offset, the height of the container, and the height of each row. Everything else in a virtual list follows from keeping those two integers up to date and rendering the matching slice.
Exercise
A list has 50,000 items, each row is 40px tall, and the container is 400px high. Roughly how many rows must be in the DOM at once for a windowed list, ignoring any extra margin rows, and why must the inner spacer still be 2,000,000px tall?
Show answer
About 10 rows (400 / 40) are visible at once, so only about 10 need to be in the DOM. The spacer must still be 50,000 × 40 = 2,000,000px tall so the scrollbar has the range and size of the full list, and so scrolling can reach every row.