← Back to Frontend, page 3

frontend-0054 · Hard senior

A page must show a list of 100,000 rows. Why does a plain render become slow, and how does list virtualization fix it?

Every DOM node adds memory, style and layout cost, so 100,000 rows make the page slow to build, scroll and update. Virtualization renders only the rows in and near the viewport, inside a container sized as if all rows existed, and swaps the rows as the user scrolls.

Full answer

A row can contain several nodes. A hundred thousand rows mean hundreds of thousands of DOM nodes. The costs are:

  • Creating nodes and attaching listeners: a long task on first render.
  • Style calculation and layout: a change near the top can re-layout a huge tree.
  • Memory: each node and its styles stay in memory.
  • Slow scrolling and a bad INP, because any interaction waits for a large main-thread job.

Virtualization (windowing) avoids creating most of the rows.

  1. Make a scroll container with a fixed height and overflow: auto.
  2. Give an inner spacer the total height (rowHeight * rowCount), so the scrollbar is correct.
  3. On scroll, compute the first and last visible index from scrollTop and the container height. Add a small buffer on each side to avoid blank flashes.
  4. Render only those rows, and place them with transform: translateY(...) at the right offset.
  5. Reuse nodes when possible (recycling) to reduce work.

Fixed row height is the easy case. Variable heights need measurement and a cache of heights, with an estimate for rows not yet seen. Libraries such as TanStack Virtual or react-window handle this, and they cover details like sticky headers and horizontal scrolling.

Costs and edge cases:

  • Browser find-in-page does not find text in rows that are not in the DOM.
  • Screen readers see only the rendered rows. Provide aria-rowcount and aria-rowindex on grid-like lists, or choose pagination for content where users need the whole list.
  • Keyboard focus can be lost when its row is removed. Keep the focused row rendered.
  • Anchors, "jump to row" and restoring scroll position need an index-based API.

Alternatives: pagination, "Load more", server-side filtering and search, and the CSS property content-visibility: auto, which skips rendering work for off-screen content but keeps all nodes in the DOM.

The page below does the fixed-height version. The test checks that fewer than 30 row nodes exist at any time, and that after scrolling to the middle the list shows the correct item.

html
<div id="scroller" style="height:400px; overflow:auto; position:relative">
  <div id="spacer"></div>
  <div id="rows" style="position:absolute; top:0; left:0; right:0"></div>
</div>
<script>
  const total = 100000, rowHeight = 40, buffer = 5;
  spacer.style.height = total * rowHeight + 'px';

  function render() {
    const first = Math.max(0, Math.floor(scroller.scrollTop / rowHeight) - buffer);
    const last = Math.min(total, Math.ceil((scroller.scrollTop + scroller.clientHeight) / rowHeight) + buffer);
    rows.style.transform = `translateY(${first * rowHeight}px)`;
    rows.replaceChildren(...Array.from({ length: last - first }, (_, k) => {
      const row = document.createElement('div');
      row.style.height = rowHeight + 'px';
      row.textContent = 'Item ' + (first + k + 1);
      return row;
    }));
  }
  scroller.addEventListener('scroll', render, { passive: true });
  render();
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The check read the number of row nodes at the top and after scrolling to the middle (about 15 to 20 nodes) and the text of the visible row. Scrolling by touch, find-in-page and screen readers were not tested.

Key points

  • Large DOM trees cost memory, style, layout and slow interaction.
  • Virtualization renders only visible rows plus a buffer.
  • A spacer with the total height keeps the scrollbar honest.
  • Variable heights need measurement. Libraries handle this.
  • Costs: find-in-page, accessibility and focus need attention.

Common mistakes

  • Rendering every row and hoping a faster framework will fix it.
  • Forgetting the buffer, which causes blank areas while scrolling quickly.
  • Virtualizing without keeping the focused row in the DOM.
  • Ignoring accessibility and find-in-page for content users must read fully.

Follow-ups

  • How would you handle rows of different heights?
  • What does content-visibility auto do, and how does it differ?
  • How would you restore the scroll position after navigation?

Tips

  • Explain why it is slow before the fix, with the four costs.
  • Mention at least two drawbacks of virtualization.