Sub topic 2
Measure first with the Profiler
This chapter assumes you know function components, useState, and how props flow between components. It also assumes you know that a component re-renders when its state or its parent changes. Later chapters add memo, useMemo, and useCallback. Each of them adds code and has a cost, so you should first confirm that a render problem exists and find where it is.
Renders are not the same as slowness
A re-render is React calling your component function again. Most re-renders are cheap, and React skips the DOM work when the output is unchanged. A component that renders 40 times is not a problem if each render takes 0.2 ms. A component that renders once for 300 ms is.
You need two numbers for each suspect component: how often it renders, and how long each render takes. The React DevTools Profiler gives you both.
The measuring workflow
Using the DevTools Profiler
Install the React Developer Tools browser extension and open the Profiler tab.
- Open the gear icon and turn on "Record why each component rendered while profiling."
- Click Record, perform the one slow interaction, and click Stop.
- Pick a commit in the bar chart at the top. A commit is one batch of changes React applied to the screen. Tall, orange or yellow bars are the slow ones.
- Read the flame chart for that commit.
| View | What it shows |
|---|---|
| Flamegraph | The component tree for one commit. Width and color show render time. Gray components did not render. |
| Ranked | Components in that commit sorted by render time, slowest first. |
| Component details | The reason for the render, such as "Props changed: items" or "Hooks changed". |
The reason is the most useful part. "Props changed" on a component whose props look identical often means an unstable prop, which a later chapter covers. "The parent component rendered" means the component re-ran only because its parent did.
Measure under realistic conditions
Development builds are much slower than production builds. React also renders components twice in development under StrictMode to detect bugs, so durations look larger than they are. Profile a production build with profiling enabled, or at least treat development numbers as relative, not absolute. Throttling the CPU by 4x in the browser's Performance tab approximates a mid-range phone.
Measuring in code with `<Profiler>`
React also ships a Profiler component that reports timings to a callback. This is useful for logging or for comparing before and after in a test.
import { Profiler, useState } from 'react';
const products = Array.from({ length: 3000 }, (_, i) => ({
id: i,
name: `Product ${i}`,
}));
function ProductList() {
return (
<ul>
{products.map((p) => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}
function onRender(id, phase, actualDuration, baseDuration) {
console.log(
`${id} ${phase}: ${actualDuration.toFixed(1)}ms (without memoization: ${baseDuration.toFixed(1)}ms)`
);
}
export function App() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
<Profiler id="ProductList" onRender={onRender}>
<ProductList />
</Profiler>
</>
);
}Each click re-renders ProductList because its parent changed. actualDuration is the time spent rendering in this commit. baseDuration estimates the cost of rendering the whole subtree with no memoization. If actualDuration is much lower than baseDuration, memoization is already helping.
Note that <Profiler> does nothing in a standard production build. Use a profiling build, or run it in development.
Common mistakes
- Counting renders instead of timing them. Many cheap renders are fine.
- Profiling the development build and optimizing a problem that does not exist in production.
- Recording a long session. Record one interaction so the slow commit is easy to find.
- Changing three things at once. You cannot tell which change helped.
- Skipping the second recording. Without a before and after number, you do not know whether the fix worked.
Exercise
In the Profiler you see a component that rendered 60 times during a typing test, but the Ranked view shows each render took under 0.3 ms. Should you wrap it in memo? Explain.
Show answer
Probably not. The total cost is about 60 x 0.3 ms = 18 ms spread across the whole interaction, which a user cannot perceive. memo adds a props comparison on every render and makes the code harder to read. Look for a component with a long render time, or a commit that blocks input, and fix that first.