Sub topic 10
Recording a profiling session
This chapter assumes you can write function components, use useState and props, and open browser developer tools. You should also have a React app running locally in development mode with React Developer Tools installed.
What the Profiler records
The Profiler tab records how long React spends rendering your components. Each time React commits a change to the screen, the Profiler stores one entry called a commit. A commit lists which components rendered and how long each took.
A recording only captures what happens between starting and stopping. This keeps the data small and focused on the interaction you care about.
Record a session
- Open your app and open the browser developer tools.
- Select the Profiler tab.
- Open the Profiler settings (the gear icon) and turn on Record why each component rendered while profiling. Later chapters use this data.
- Click the round Start profiling button. It turns red.
- Perform one specific interaction in your app, such as clicking an item in a list.
- Click the button again to stop.
React Developer Tools then shows the commits from that interaction. Keep each recording short. One interaction per recording makes the results easy to read.
A component to profile
Use this list as a test subject. Every click changes state in the parent, and the profiler will show which items render in response.
import { memo, useState } from 'react';
const Item = memo(function Item({ label, onSelect }) {
return <li onClick={() => onSelect(label)}>{label}</li>;
});
export function List({ items }) {
const [selected, setSelected] = useState(null);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item
key={label}
label={label}
onSelect={(value) => setSelected(value)}
/>
))}
</ul>
</div>
);
}Start a recording, click one item, and stop. You should see a commit for the click. Because onSelect is a new function on every render, every Item renders again even though memo is applied. The next chapters explain how to read that result and fix it.
Navigating the recording
After you stop, the Profiler shows a row of vertical bars near the top right. Each bar is one commit.
- Taller, yellower bars took longer to render. Short, blue bars were fast.
- Click a bar, or use the arrow buttons, to move between commits.
- The commit number, such as
1 / 3, tells you where you are. - Gray bars in the chart mean the component did not render in that commit.
If no commits appear, nothing rendered during the recording. Check that you actually triggered a state change.
Common errors
- The Profiler tab is missing or says profiling is not supported. Production builds usually do not include profiling support. Use a development build, or a production build with profiling enabled.
- Timings look much slower than the live app. Development builds do extra checks. Use the numbers to compare components, not as final performance figures.
- Recording too much. A long session produces many commits, so finding the one that matters is harder. Stop and start again with a single action.
- No render reasons. If you did not enable the settings option before recording, the reasons are missing. Turn it on and record again.
Exercise
Exercise
You start a recording, click one list item, and stop. The Profiler shows commits, but the "why did this render" information is empty. What did you most likely forget, and what do you do?
Show answer
You did not turn on "Record why each component rendered while profiling" in the Profiler settings before recording. Turn it on, then start a new recording and repeat the click.
Exercise
Why is it better to record one interaction per session than to click around the app for a minute?
Show answer
A short recording has few commits, so each commit is easy to match to an action. A long recording mixes many commits together and makes it hard to find the one that caused a problem.