Sub topic 4
Recording a Session with the React DevTools Profiler
This chapter assumes you can build function components with state and hooks, and that you know how to open your browser's developer tools. It also assumes you know that React renders first and commits second. Here you will record a profiling session and see what the recording contains. The next chapter covers how to read the charts.
What the Profiler records
The React DevTools Profiler is a tab in the React DevTools browser extension. While you record, it saves one entry for each commit React makes. For each commit it stores which components rendered and how long each took. It does not sample the CPU continuously, so it shows only React work. Other work, such as layout or your own event handlers, does not appear.
Set up
- Install the React DevTools extension for Chrome, Edge, or Firefox.
- Run your app in development mode and open the page.
- Open the browser developer tools. You should see two new tabs, Components and Profiler.
If the tabs are missing, reload the page. The extension attaches only to pages where it detects React.
A component worth profiling
Use something that reacts to input and does visible work. This component is slow on purpose:
import { useState } from "react";
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 2) {} // simulate 2ms of work
return <li>{text}</li>;
}
export default function App() {
const [query, setQuery] = useState("");
const items = Array.from({ length: 100 }, (_, i) => `Item ${i}`);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<ul>
{items.map((text) => (
<SlowItem key={text} text={text} />
))}
</ul>
</>
);
}Each keystroke updates state in App, so all 100 items render again.
Record a session
- Open the Profiler tab and click the blue circle to start recording.
- Do one specific thing in the page, such as typing three characters in the input.
- Click the circle again to stop.
Keep sessions short and focused on a single interaction. A long recording buries the commit you care about.
After you stop, the top right shows a bar chart with one bar per commit. Taller, warmer bars took longer. Gray bars are commits where the selected component did not render. For the example above, you should see three bars, one per keystroke. Use the arrows or click a bar to select a commit.
Settings that change what you capture
Click the gear icon in the Profiler tab to open the settings.
| Setting | Effect |
|---|---|
| Record why each component rendered while profiling | Saves the reason for each render. You need it for the "why did this render" chapter. Turn it on before recording. |
| Hide commits below N ms | Filters out tiny commits so the chart shows only meaningful ones. |
Use Reload and start profiling (the circular arrow button) when you need to measure the initial mount, since the first render happens before you can click Record.
Common errors
- Timings look too slow. Development builds add checks, so their durations are inflated. Compare them with each other, not with real-user numbers. The Profiler is disabled in standard production builds, so use a profiling build when you need production-like timings.
- The recording is empty. No commit happened. Check that your action actually changed state.
- The reason for a render is missing. The reasons setting was off while you recorded. Turn it on and record again, because the data cannot be recovered afterward.
Exercise
You recorded a session, but the Profiler shows no reason for why SlowItem rendered. What went wrong, and what do you do?
Show answer
The setting "Record why each component rendered while profiling" was off during the recording. Turn it on in the Profiler settings, then record the interaction again.