Virtual Scrolling — Performance

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…

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.

  1. The scroll box has a fixed height (500 px) and overflow-y: auto. Its scroll position is read from scrollTop, how many px of the content have rolled up out of view.
  2. 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.
  3. Only the visible rows, wrapped in one element that is moved down by offsetY with transform: translateY(...), so those rows appear right at the scroll position.
  4. On every scroll event, work out the rows again from scrollTop, then swap the content.

Working out which rows to render

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 }

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();

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

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.

What you will learn