Sub topic 1
Why Long Lists Get Slow
Rendering a few thousand rows in React can freeze a page. This chapter explains where that cost comes from, so the later chapters on windowing make sense. It covers the cost model only. Building a virtual list comes later.
Prerequisites
This chapter assumes you know:
- React function components, props, and state.
- The
useState,useRef, anduseEffecthooks. - Basic CSS positioning (
relative,absolute) and overflow scrolling. - How the browser DOM and scroll events work.
The naive list
The simplest list maps an array to elements:
export function NaiveList({ items }: { items: string[] }) {
return (
<ul>
{items.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
);
}This is fine for 50 rows. With 50,000 rows, React creates 50,000 elements, and the browser creates 50,000 DOM nodes. Most of them are far outside the screen, and nobody sees them.
Where the time goes
Rendering a long list costs time in several places. Each stage does work for every row, visible or not.
- React render. Your component function runs and builds an element for each row. Any row components you add run too.
- Reconciliation. React compares the new elements with the previous ones to decide what changed.
- DOM creation. On first mount, the browser allocates a node for every row and its children.
- Style and layout. The browser works out which CSS rules apply to each node, then computes each node's size and position.
- Paint and composite. The browser draws the pixels. This step mostly covers visible content, so it is usually the cheapest.
The first four steps scale with the number of rows, not with the number of visible rows. Doubling the list roughly doubles the work.
Why it keeps hurting after the first render
The first mount is only part of the problem. The cost returns whenever the list updates.
- Parent re-renders. If a parent component re-renders, React calls your list component again. Without memoization, every row re-renders.
- State changes. Selecting one row or typing in a filter box can trigger a render of all rows.
- Layout work. Changing one row's height can force the browser to recompute the layout of the rows below it.
- Memory. Each DOM node uses memory. Tens of thousands of nodes slow garbage collection and can crash low-end devices.
How many rows is too many?
There is no fixed limit. The answer depends on how complex each row is, and on the user's device.
| Row complexity | Rough point where problems appear |
|---|---|
| Plain text, one element | Several thousand rows |
| Card with image, buttons, and handlers | Hundreds to a low thousand rows |
| Row with nested components and state | Often under a few hundred rows |
These figures are rough guides, not benchmarks. A mid-range phone hits them much sooner than a developer laptop. Measure on the hardware your users have. The next chapter shows how.
The idea that fixes it
The user sees only about 10 to 30 rows at a time. The browser and React spend time on thousands more. Windowing removes that wasted work by rendering only the rows near the viewport, and the number of DOM nodes stays roughly constant however long the list is. The chapters that follow build this up step by step.
Exercise
A list of 20,000 rows renders slowly. You wrap each row in React.memo, and the page gets faster when you select a row, but the first load is still slow and memory use is high. Why?
Show answer
React.memo only skips re-rendering rows whose props did not change. The first load still has to render all 20,000 rows, create 20,000 sets of DOM nodes, and run style and layout for all of them. Those nodes also stay in memory. Only reducing the number of rendered rows, for example by windowing, removes those costs.