Sub topic 14
Measuring renders with the React DevTools Profiler
This chapter assumes you know how renders are triggered, how the render and commit phases differ, and how React.memo compares props. It builds on the memoization chapter. Memoization only helps when it prevents work you can measure, and the Profiler is the tool that measures it.
Why measure before optimizing
A component that re-renders is not a bug. Rendering is a function call, and most calls take microseconds. Trouble starts when one update re-renders hundreds of components, or when one component does expensive work on every render. Guessing which case you have usually leads to useMemo calls that add code and fix nothing.
The Profiler records real commits and tells you three things:
- Which components rendered in each commit.
- How long each one took.
- Why each one rendered, if you enable that option.
Setting up
The React Developer Tools browser extension adds a Profiler tab next to Components. Three details affect whether your numbers mean anything:
- Development builds are much slower than production builds. They add extra checks and warnings. Use the timings to compare components with each other, not as absolute figures for your users.
- Strict Mode renders components twice in development. DevTools hides the second render's console logs by default, but the extra work still runs. Remember this when a component looks twice as slow as expected.
- A normal production build does not include profiling support. To profile production-like code, use a profiling build. For
react-dom, that means aliasingreact-dom/clienttoreact-dom/profilingin your bundler. Frameworks often expose a flag for this.
Before you record, open the Profiler settings (the gear icon). Under General, turn on Record why each component rendered while profiling. Without it you see that a component rendered, but not the cause.
Recording a session
Record one interaction at a time. A good session looks like this:
- Load the page and let it settle.
- Click the blue record button.
- Perform one action, such as typing a character or opening a menu.
- Click stop.
Short sessions are easier to read. If you record a minute of mixed activity, you get dozens of commits and cannot tell which one belongs to which action.
The result is a list of commits. Each bar in the commit selector at the top is one commit, which is one pass of render, then commit, to the DOM. A taller, warmer bar means a longer commit. Commits where nothing rendered are not recorded.
Reading the flamegraph
The Flamegraph view shows the component tree for the selected commit. Each box is a component, and its width is the time spent rendering that component and its children.
| Box appearance | Meaning |
|---|---|
| Gray | The component did not render in this commit. React skipped it. |
| Blue to green | Rendered quickly relative to the slowest component. |
| Yellow to orange | Rendered slowly relative to the slowest component. |
| Wide box, narrow children | The time is in this component's own code. |
| Wide box, wide children | The time is spread across its subtree. |
Colors are relative to the commit, not absolute. A commit where the slowest component took 0.4 ms still shows an orange box.
Gray boxes confirm that memoization worked. If you wrapped Row in memo and the rows are gray after a parent state change, React skipped them.
Using the ranked chart and the render reasons
The Ranked view sorts components in the selected commit by their own render time, slowest first. Use it when the flamegraph is large and you want the hot spots.
Click a component in either view and the right-hand panel shows details. With render reasons enabled, you see entries such as:
- Props changed, with the names of the props that differed.
- State changed, or Hooks changed, with the hook index.
- The parent component rendered.
- Context changed.
"The parent component rendered" on a memoized component is the clue that a prop is changing identity every render. In that case the message normally says which props changed, even though their values look the same. Typical causes are inline objects, arrays, and arrow functions created in the parent.
Two other buttons help while you explore. The Components tab has a setting called Highlight updates when components render. It draws a colored outline around every component that re-renders in the page, which is a quick visual check before you open the Profiler.
A worked example
This component renders a list of 500 memoized rows. The parent also has an input whose text lives in state.
import { Profiler, memo, useState } from 'react';
function onRender(id, phase, actualDuration, baseDuration) {
console.log(
`${id} ${phase}: actual ${actualDuration.toFixed(1)}ms, base ${baseDuration.toFixed(1)}ms`
);
}
const Row = memo(function Row({ id, style }) {
return <li style={style}>Row {id}</li>;
});
export default function App() {
const [text, setText] = useState('');
const ids = Array.from({ length: 500 }, (_, i) => i);
return (
<Profiler id="List" onRender={onRender}>
<input value={text} onChange={e => setText(e.target.value)} />
<ul>
{ids.map(id => (
<Row key={id} id={id} style={{ color: 'gray' }} />
))}
</ul>
</Profiler>
);
}Record one keystroke. The flamegraph shows every Row in color, not gray. The render reason for a row reads "Props changed: style". The object literal { color: 'gray' } is a new object on every render of App. memo compares props with Object.is, so each row sees a "different" style and renders again.
The fix is to give the object a stable identity. Here, hoisting it out of the component is enough.
const ROW_STYLE = { color: 'gray' };
// inside App:
// <Row key={id} id={id} style={ROW_STYLE} />Record the same keystroke again. The rows are now gray, and only App and the input's subtree rendered. The commit's bar in the selector is shorter, and the measurement, not a hunch, tells you the change helped.
Measuring in code with the Profiler component
DevTools needs a person at a browser. For automated checks, or for sending measurements from a staging site, React provides the <Profiler> component. It wraps a part of the tree and calls onRender after every commit that updates that subtree.
The callback arguments are:
| Argument | Meaning |
|---|---|
| `id` | The `id` prop you gave this Profiler, so you can tell several apart. |
| `phase` | `"mount"`, `"update"`, or `"nested-update"` (an update caused during a commit). |
| `actualDuration` | Time spent rendering the wrapped subtree in this commit. |
| `baseDuration` | Estimated time to render the whole subtree with no memoization. |
| `startTime`, `commitTime` | Timestamps for when React began rendering and committed the update. |
Compare actualDuration with baseDuration. If actualDuration is much smaller, memoization is skipping most of the subtree. If they are about equal on every update, memoization is doing nothing there.
The measurement loop
Profiling works best as a loop with a fixed order of steps.
Change one thing per iteration. If you add three useMemo calls at once and the commit gets faster, you do not know which one helped, and the other two stay in your code as noise.
Common mistakes and edge cases
- Profiling with throttling off. Fast developer machines hide problems. In the browser's Performance settings, apply CPU throttling (for example 4x or 6x slowdown) to approximate a mid-range phone.
- Blaming the wrong phase. The Profiler reports render time. Slow DOM updates, layout, and paint happen in the commit and after it, and they show up in the browser's Performance panel instead. A fast render with a janky screen points there.
- Chasing every re-render. A component that renders in 0.1 ms and re-renders needlessly costs nothing you can notice. Fix renders that are slow, frequent, or both.
- Comparing numbers across runs. Timings vary between runs. Record the same action three times and compare the typical value.
- Effects hidden in the numbers. Effects run after the commit, so their cost is not part of
actualDuration. An effect that sets state causes a second commit, which you see as a separate bar in the commit selector. - Memoization that costs more than it saves. Comparing props takes time too. For a tiny component, the comparison can cost as much as rendering it, and
baseDurationwill tell you the subtree was cheap to begin with.
Summary of the workflow
- Enable render reasons and record one interaction.
- Use the flamegraph to see which components rendered (colored) and which were skipped (gray).
- Use the ranked chart to find the slowest components.
- Read the render reason. "Props changed" with unstable object or function props points to missing
useMemooruseCallback, or to a value that can be hoisted. - Change one thing and record again.
- Keep the change only if the commit got measurably faster.
Exercise
You wrapped a list item component in memo, but the Profiler shows every item rendering after the parent's state changes. The render reason reads "Props changed: onSelect". What is the likely cause, and how do you confirm the fix worked?
Show answer
The parent creates a new onSelect function on every render, so memo's shallow comparison sees a different prop each time. Wrap the handler in useCallback with the right dependencies, or define it outside the component if it uses no state. To confirm, record the same interaction again: the items should be gray in the flamegraph, and the commit should be shorter than before.
Exercise
A <Profiler> callback reports actualDuration of 12 ms and baseDuration of 13 ms on every update. What does this say about memoization in that subtree?
Show answer
It is skipping almost nothing. actualDuration is the time actually spent, and baseDuration is the estimated time with no memoization. Values this close mean nearly all components in the subtree render on each update, so either no memoization is present or its props change identity every render.