A list of 1,000 rows, each row 50 px tall, in a list box 500 px tall. How many rows need to be in the DOM for the scroll to still feel full? Not 1,000. Not 10 either.
Virtual scrolling (also called windowing) is a technique that renders only the rows you can see from a long list, even when there are thousands of items. Picture riding a train: the view outside goes on for kilometres, but only a slice of it passes the window. The window doesn't need to hold the whole view, just the slice in front of it right now. As the train moves, the slice changes.
The problem: every row goes into the DOM
If all 10,000 rows are rendered, the browser has to create 10,000 elements, style them, compute their layout and keep all of them in memory. Yet a 500 px box only shows about 10 rows. The page shows up slowly, wastes memory, and every time the layout is recomputed, those 10,000 elements are part of it.
How it works
Virtual scrolling separates two things: the height the scrollbar feels, and the rows that really exist.
- The scroll box has a fixed height (500 px) and
overflow-y: auto. Its scroll position is read fromscrollTop, how many px of the content have rolled up out of view. - A spacer, an empty element as tall as all the rows: 1,000 × 50 = 50,000 px. Thanks to it, the scrollbar feels like a full list.
- Only the visible rows, wrapped in one element that is moved down by
offsetYwithtransform: translateY(...), so those rows appear right at the scroll position. - On every
scrollevent, work out the rows again fromscrollTop, then swap the content.
Working out which rows to render
startIndex = Math.floor(scrollTop / itemHeight): the first visible row.Math.ceil(containerHeight / itemHeight) + 1: how many rows to render. 500 / 50 = 10 rows fill the box exactly, plus 1 spare row. WhenscrollTopisn't a multiple of 50, the top row is cut off and one more row peeks in at the bottom. Without that spare, the bottom of the box is empty.endIndex = Math.min(startIndex + rows, totalItems): the last index plus 1, so rowendIndexitself isn't included.Math.minkeeps it from going past the number of items.offsetY = startIndex * itemHeightandvisibleCount = endIndex - startIndex.
scrollTop |
startIndex |
endIndex |
offsetY |
visibleCount |
Rows in the DOM |
|---|---|---|---|---|---|
| 0 | 0 | 11 | 0 | 11 | 0 to 10 |
| 2500 | 50 | 61 | 2500 | 11 | 50 to 60 |
| 2525 | 50 | 61 | 2500 | 11 | 50 to 60 |
| 49500 | 990 | 1000 | 49500 | 10 | 990 to 999 |
Out of 1,000 items, only 11 rows are in the DOM. With 10,000 items it's still 11. In the last row of the table, 990 + 11 = 1001 goes past 1,000, so Math.min cuts it to 1000 and only 10 rows are rendered.
function getVisibleItems(scrollTop, containerHeight, itemHeight, totalItems) {
const startIndex = Math.floor(scrollTop / itemHeight);
const rows = Math.ceil(containerHeight / itemHeight) + 1; // +1 spare row
const endIndex = Math.min(startIndex + rows, totalItems);
return {
startIndex,
endIndex,
offsetY: startIndex * itemHeight,
visibleCount: endIndex - startIndex,
};
}
console.log(getVisibleItems(2500, 500, 50, 1000));
// { startIndex: 50, endIndex: 61, offsetY: 2500, visibleCount: 11 }
Math.floorrounds down: ascrollTopof 2525 still starts at row 50.Math.ceilrounds up, so a box whose height isn't an exact multiple of the row height is still full.- The object is written with its keys in the order
startIndex,endIndex,offsetY,visibleCount.
Wiring it into the page
const list = document.querySelector("#list"); // 500px tall, overflow-y: auto
const rows = document.querySelector("#rows"); // inside a spacer 50,000px tall
function render() {
const { startIndex, endIndex, offsetY } = getVisibleItems(list.scrollTop, 500, 50, 1000);
const fragment = document.createDocumentFragment();
for (let i = startIndex; i < endIndex; i++) {
const row = document.createElement("div");
row.className = "row"; // height: 50px
row.textContent = "Row " + i;
fragment.appendChild(row);
}
rows.replaceChildren(fragment);
rows.style.transform = `translateY(${offsetY}px)`;
}
list.addEventListener("scroll", render);
render();
replaceChildren(fragment)throws out the old rows and puts the new ones in at once.translateY(offsetY)puts the first row exactly where row numberstartIndexsits inside the spacer.- In React, libraries like
@tanstack/react-virtualorreact-windowdo the same arithmetic for you.
When to use it
Use it for lists of hundreds to millions of rows: log tables, feeds, chat history, spreadsheets. For 30 or 50 rows, a normal render is enough. Virtual scrolling adds complexity, and rows that aren't in the DOM can't be found with the browser's Ctrl+F.
⚠️ Arithmetic that leaves holes in the list
- Rows with different heights. The formula
scrollTop / itemHeightis only right when every row has the same height. When heights vary, each row's position has to be measured or estimated first, and that's what the libraries do. - Forgetting the spare row. Without
+ 1, ascrollTopof 2525 leaves an empty 25 px gap at the bottom. endIndexgoing past the number of items. At ascrollTopof 49500, withoutMath.minthe result is 1001. It must be 1000, with avisibleCountof 10.- Keys with a different name or order. The exercise compares the object exactly:
startIndex,endIndex,offsetY,visibleCount, in that order.
TL;DR: Virtual scrolling only renders the visible rows, plus a few spares, inside a spacer as tall as all the rows so the scrollbar stays right. The formula: startIndex = Math.floor(scrollTop / itemHeight), endIndex = Math.min(startIndex + Math.ceil(containerHeight / itemHeight) + 1, totalItems), offsetY = startIndex * itemHeight, visibleCount = endIndex - startIndex. A 500 px box with 50 px rows needs only 11 rows in the DOM, whether there are 1,000 items or 10,000. The condition: every row's height is consistent or can be measured. For short lists, just render normally.
⚡ Only what you can see
Guess first: what is the main idea of virtual scrolling?
Render only the items visible in the viewport, not the whole list. A 500 px box with 50 px rows needs only 11 rows in the DOM, whether there are 1,000 items or 10,000.
Meaning: An empty spacer as tall as all the rows (1,000 × 50 = 50,000 px) keeps the scrollbar feeling full, and the visible rows are moved into place with transform: translateY(...).
⚡ Work out the rows from scrollTop
Write:
const startIndex = Math.floor(scrollTop / itemHeight);
const rows = Math.ceil(containerHeight / itemHeight) + 1; // +1 spare row
const endIndex = Math.min(startIndex + rows, totalItems);
Meaning: The spare row fills the gap at the bottom when scrollTop isn't a multiple of 50. Math.min keeps endIndex from going past the number of items: at a scrollTop of 49500 it's 1000, with a visibleCount of 10.
⚡ When to use it
Meaning: For lists of hundreds to millions of rows, whose row heights are consistent or can be measured. For 30 or 50 rows, a normal render is enough.