# List Virtualization

> How to render lists of 10,000+ items without crashing the browser by only rendering what's visible on screen.

Subject: React | Lesson 34 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/list-virtualization
Previous: [Code Splitting & <Suspense>](https://codewithanimation.in/react/code-splitting-suspense.md) | Next: [Concurrency in React 18](https://codewithanimation.in/react/concurrent-features.md)

## The Problem with Huge Lists

If you try to render a `<ul>` with 10,000 `<li>` elements in React, the browser will likely freeze or crash. The DOM (Document Object Model) is heavy, and forcing the browser to calculate the layout and paint 10,000 nodes at once is incredibly expensive.

List Virtualization (also known as 'windowing') solves this by rendering *only* the items that currently fit inside the user's viewport, plus a small buffer above and below.

## The Illusion of Scrolling

Virtualization works by creating a massive, empty 'spacer' `div` that represents the total height of all 10,000 items (e.g., `height: 500,000px`). This gives the browser a realistic scrollbar.

Inside that spacer, it absolutely positions only the ~20 items the user can actually see.

As the user scrolls, it doesn't create new DOM nodes. It simply takes the DOM node that scrolled out of view at the top, moves it to the bottom, and updates its data. The total number of DOM nodes stays constant (e.g., 20), no matter how big the list gets.

## Using Libraries

You rarely build virtualization from scratch. Instead, you use established libraries like `react-window` or `react-virtuoso`.

`import { FixedSizeList } from 'react-window';`

`<FixedSizeList height={400} itemCount={1000} itemSize={50}>`

`  {({ index, style }) => <div style={style}>Row {index}</div>}`

`</FixedSizeList>`

Notice how you pass the `style` object to your row. This contains the absolute positioning (the `top` value) calculated by the library based on the current scroll position.

## Common mistake: Dynamic Heights

Virtualization is easy when every row is exactly 50px tall (`FixedSizeList`).

It becomes incredibly difficult when rows have dynamic heights (e.g., a Twitter feed where some tweets have images and some don't). Libraries like `react-virtuoso` handle dynamic heights by measuring the DOM node after it renders and caching its height, but this requires more CPU overhead.

## Tip: The Accessibility Trap

Because virtualized lists remove items from the DOM, screen readers (and native browser search `Ctrl+F`) cannot 'see' the items that aren't currently rendered.

You must provide alternative search mechanisms (like a custom search bar) if users need to find data within the unrendered parts of the list.

## Try it: The Viewport Window

Interact with the slider below to 'scroll' through a list of 100 items. Notice how the actual number of DOM nodes stays constant—the nodes just update their data and leapfrog each other as you scroll!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/list-virtualization

## Check yourself

### Question 1

If you have a virtualized list of 1,000,000 items, and the viewport can fit 10 items, roughly how many DOM nodes are actually rendered by the browser?

- A. 1,000,000
- B. Around 15 (10 visible + a small buffer)
- C. 0, everything is painted to a canvas

Correct answer: B

Virtualization limits the DOM size to exactly what is visible on the screen, plus a tiny overscan buffer to prevent flickering when scrolling quickly.

- Why not A: That would crash the browser.
- Why not C: Virtualization still uses normal HTML DOM nodes.

### Question 2

How does a virtualized list maintain a realistic scrollbar if only 10 items are rendered?

- A. By overriding the browser's native scrollbar with CSS.
- B. By rendering a giant empty `div` that matches the total calculated height of all items combined.
- C. It doesn't; you have to use pagination instead of scrolling.

Correct answer: B

The library calculates `itemCount * itemSize` and creates an invisible spacer. The few rendered items are then absolutely positioned within that giant spacer based on the current scroll offset.

### Question 3

When using a library like `react-window`, why must you apply the provided `style` object to your row component?

- A. To make the row look pretty with default colors.
- B. To give the row the correct absolute positioning (`top`, `height`, etc.) so it appears in the right place within the scroll container.
- C. To tell React that the component is memoized.

Correct answer: B

Because the items are placed inside a giant empty container, they must be absolutely positioned. The library calculates the exact `top` pixel value for each item based on its index.

## Remember this

- Rendering thousands of DOM nodes will crash the browser.
- List virtualization only renders the items currently visible in the viewport.
- It works by using a giant spacer `div` and absolutely positioning a small number of recycled DOM nodes.
- Use libraries like `react-window` or `react-virtuoso` instead of writing it yourself.
- Beware of accessibility issues: `Ctrl+F` will not find unrendered items.
