Sub topic 2
Measure before you optimize
This chapter assumes you know React function components and hooks, and that you can open browser DevTools. You will learn what to measure on a long list, how to take a baseline, and how to decide whether virtualization is worth it.
Why measure first
Virtualization adds code, edge cases, and accessibility costs. A list of 200 simple rows usually renders fast enough without it. Measuring tells you whether you have a problem and gives you a number to compare against after your fix.
What to measure
Three numbers cover most list problems:
| Metric | What it tells you | Where to find it |
|---|---|---|
| DOM node count | How much the browser must lay out and paint | Console, `document.querySelectorAll("li").length` |
| React render time | How long React spends rendering the list | React Profiler |
| Scroll smoothness | Whether frames are dropped while scrolling | DevTools Performance panel |
Node count is the quickest check. Render time and scroll smoothness show what the nodes cost you.
Measure render time with the Profiler
React's Profiler component calls a function each time the tree inside it commits. The actualDuration argument is the time spent rendering that tree, in milliseconds.
import { Profiler, type ProfilerOnRenderCallback } from "react";
const onRender: ProfilerOnRenderCallback = (id, phase, actualDuration) => {
console.log(`${id} ${phase}: ${actualDuration.toFixed(1)} ms`);
};
export function MeasuredList({ items }: { items: string[] }) {
return (
<Profiler id="list" onRender={onRender}>
<ul>
{items.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</Profiler>
);
}The phase value is "mount" for the first render and "update" for later ones. Render this with 10,000 strings and note both values.
Measure scrolling in DevTools
Render time does not capture layout and paint work, which often dominates for large DOMs. Use the Performance panel:
- Open DevTools and select the Performance tab.
- Click Record, scroll the list quickly for a few seconds, then stop.
- Look at the frame rate and the long tasks. Frames that take more than about 16 ms miss a 60 fps target.
- Open a long task and check whether the time goes to scripting, layout, or paint.
A repeatable baseline
Change one thing at a time and keep conditions the same. A simple routine:
- Use the same data size, such as 10,000 rows.
- Throttle the CPU in DevTools (4x slowdown) to approximate a mid-range phone.
- Record the node count, the mount time, and the dropped frames during a scroll.
- Repeat each measurement three times and compare medians, since single runs vary.
Reading the results
Common patterns and what they suggest:
- High node count, slow mount, slow scroll: the list is too large for the DOM. Virtualization is a good fit.
- Low node count, slow render: rows are expensive. Fix the row component first, for example with memoization or by removing heavy work from render.
- Fast initial render, slow updates: something re-renders the whole list on each change, such as a state update in a parent. Fix that first.
Virtualization only helps the first pattern. Applying it to the others hides the cost without removing it.
Common mistakes
- Measuring only in development mode and treating the numbers as absolute.
- Testing on a fast desktop with no throttling.
- Comparing runs with different data sizes.
- Optimizing before confirming that the list is the slow part.
Exercise
A list of 300 rows takes 4 ms to render, scrolls at 60 fps, and has 900 DOM nodes. A teammate proposes virtualization. What do you recommend, and why?
Show answer
Do not virtualize yet. The render time is low, scrolling is smooth, and the node count is moderate, so there is no measured problem. Virtualization would add complexity and accessibility trade-offs for no benefit. Re-measure if the list grows or the target devices get slower.