Sub topic 14
Measuring renders with the React DevTools Profiler
Earlier chapters covered why components re-render and how React.memo, useMemo, and useCallback can skip work. This chapter shows how to check whether a re-render is a real problem before you add any of those tools. It assumes you can write function components, use useState, and have read the chapter on memoization.
Why measure first
A re-render is not a bug. Calling a component function is usually cheap, and React only touches the DOM when the output changes. Memoization adds its own cost: React must compare props or dependencies on every render. If you add it without measuring, you get more code and often no speedup.
The React DevTools Profiler records what rendered during an interaction and how long each component took. It is part of the React Developer Tools browser extension. Use a development build for reading the "why did this render" data. Use a production build with profiling enabled when you want timings close to what users see.
Recording a profile
- Open your app and open the browser DevTools. Select the Profiler tab added by the extension.
- In the Profiler settings, enable Record why each component rendered while profiling.
- Click the record button.
- Perform one specific interaction, such as typing in a field or selecting a row.
- Click stop.
Keep each recording to one interaction. A long recording mixes many commits and hides the cause.
Reading the results
Each commit appears as a bar in the top-right chart. Taller, warmer bars took longer. Click a bar to inspect that commit.
- Flamegraph shows the component tree. Width is render time. Colored bars rendered in this commit. Gray bars did not render.
- Ranked lists components from slowest to fastest, which is the quickest way to find the most expensive one.
- Why did this render? appears when you select a component. Typical answers are "Props changed: onSelect", "State changed", "Hooks changed", or "The parent component rendered".
The last answer matters most. If a Row shows "The parent component rendered" and its props did not change, it is a memoization candidate.
A worked example
Use the List and Row code from the memoization chapter. Without memo and useCallback, selecting one row re-renders every Row. A profile of one click shows all rows colored, each with the reason "The parent component rendered".
After wrapping Row in memo and stabilizing handleSelect with useCallback, record the same click again. Only the parent List is colored, and the rows are gray.
Now check the difference in numbers. For 20 rows that each take 0.1 ms, the saving is about 2 ms, which nobody can notice. For 2,000 rows, or rows that take 5 ms each, the saving is large. The profile tells you which case you are in.
Common mistakes
- Profiling in development and trusting the absolute times. Development builds are slower, and Strict Mode renders components twice. Compare relative costs.
- Recording a long session instead of one interaction.
- Memoizing every component. This adds comparison cost where nothing is saved.
- Fixing a render that is fast. Only act on commits that are slow enough to affect the interaction.
Exercise
In a profile of one click, a memoized Row is colored and its reason is "Props changed: onSelect". The parent passes onSelect={id => setSelected(id)}. What is the cause, and how do you fix it?
Show answer
The inline arrow function creates a new function on every parent render, so memo's shallow comparison sees a changed prop. Wrap the handler in useCallback with an empty dependency array (or pass setSelected directly). Then record again and confirm that the rows are gray.
Exercise
A component takes 0.05 ms to render and re-renders 10 times per interaction. Should you memoize it?
Show answer
No. The total cost is about 0.5 ms, so memoizing saves almost nothing and adds a props comparison. Memoize only after a profile shows a cost that affects the interaction.