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 have used browser developer tools before. It also assumes you know the render and commit phases from the earlier chapter. By the end you will be able to record a Profiler session, move through its commits, save it for later, and avoid the setup mistakes that produce empty or misleading recordings.
What the Profiler records
The React DevTools browser extension adds two tabs to your browser's developer tools: Components and Profiler. Components shows the current tree. Profiler records what happened over a span of time.
During a recording, React notes every commit, the moment it applies a rendering result to the DOM. For each commit it saves:
- which components rendered
- how long each one took
- optionally, why each one rendered
Render passes that produce no commit are not shown. The Profiler is a list of commits, and each commit is a snapshot of the tree with timings attached.
Install the extension for Chrome, Edge, or Firefox. Open your app, then open developer tools. The Profiler tab appears only on pages that use React in a build DevTools can instrument. The next section covers that requirement.
Choose the right build first
The development build of React includes extra checks and warnings, so its timings run higher than production. The standard production build has no profiling hooks. The Profiler tab then shows a message that profiling is unsupported, or the DevTools icon turns gray.
You have two reasonable choices:
| Build | Timings | Profiler works | Use it for |
|---|---|---|---|
| Development | Inflated, with extra checks | Yes | Finding what renders and why |
| Production | Realistic | No | Shipping |
| Production with profiling | Realistic | Yes | Measuring real cost |
Use the development build to find what re-renders. Use a profiling build to judge how much it costs. Compare relative numbers inside one build and never mix numbers across builds.
To make a profiling build with webpack, alias react-dom to its profiling entry point:
// webpack.profiling.config.js
const prod = require("./webpack.config.js");
module.exports = {
...prod,
resolve: {
...prod.resolve,
alias: {
...(prod.resolve && prod.resolve.alias),
"react-dom$": "react-dom/profiling",
"react-dom/client$": "react-dom/profiling",
},
},
};This sketch assumes React 18 or later and an existing production config at ./webpack.config.js. Older React versions (16 and 17) also need an alias from scheduler/tracing to scheduler/tracing-profiling. Other bundlers use the same idea: point the react-dom imports at react-dom/profiling. Keep this build for measuring and do not deploy it, because it carries a small overhead.
A small app to record
Use this app to follow the steps. It has one deliberately slow child and a counter that has nothing to do with it.
import { useState } from "react";
import { createRoot } from "react-dom/client";
function SlowItem({ n }) {
const start = performance.now();
while (performance.now() - start < 2) {
// Busy-wait for 2 ms to simulate expensive rendering.
}
return <li>Item {n}</li>;
}
function SlowList() {
const items = Array.from({ length: 30 }, (_, i) => i);
return (
<ul>
{items.map((n) => (
<SlowItem key={n} n={n} />
))}
</ul>
);
}
function App() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
<SlowList />
</>
);
}
createRoot(document.getElementById("root")).render(<App />);Each click re-renders App, which re-renders SlowList, which re-renders all 30 items at about 2 ms each. The click should cost about 60 ms.
Configure the Profiler before recording
Click the gear icon in the Profiler tab and open the Profiler section of the settings. Two options matter:
- Record why each component rendered while profiling. Turn this on. It adds small overhead and saves the reason for each render, such as changed props, changed state, or a parent re-render. The "Finding out why a component rendered" chapter uses this data.
- Hide commits below N ms. Leave this unset for now. It removes tiny commits from the list. That is useful in busy apps, but it can hide a flood of fast commits that add up.
Record a session
The workflow has four steps:
- Click the blue circle to start recording. The circle turns red.
- Perform the interaction you care about, and only that one. Here, click the button three times.
- Click the red circle to stop.
- Review the commits.
Keep recordings short and focused. A one-minute session with many unrelated actions produces dozens of commits, and finding the one that matters becomes harder than recording again. Record one interaction at a time and give each recording a purpose: "typing in the search box" or "opening the modal."
Recording the initial mount
The mount happens at page load, before you can click Record. Use the Reload and start profiling button, the circular arrow next to the record button. It reloads the page with profiling already active, so the first commit is captured. Without it, you can only profile updates.
Read the commit list
After you stop, the top right of the Profiler shows a bar chart. Each bar is one commit.
- Bar height and color show how long that commit took. Taller, warmer bars (yellow to orange) are slower. Short, cool bars (blue to green) are fast.
- The selected commit is highlighted. Click a bar, or use the arrow buttons beside the commit counter, to step through them.
- The commit counter, for example "2 / 4", tells you where you are.
For the sample app, a recording of three clicks gives three similar tall bars. A mount recorded with reload gives one more. The selected commit's view then shows the components that rendered, with the slow list items standing out. The next chapter covers reading that view in detail.
Components that did not render in the selected commit are drawn in gray. This is useful: it shows which parts of the tree React skipped.
Two pieces of information appear when you select a commit:
- The commit's duration and the time it began.
- For each component, the render duration (the time the component and its children took, as measured when it rendered).
Use the commit duration as a quick filter. A commit of 3 ms is almost never your problem. A commit of 60 ms during a click is a visible lag.
Save and share a profile
The Profiler can export a recording as a JSON file with the down-arrow button, and import one with the up-arrow button. This helps in three situations:
- A teammate wants to see your recording without reproducing the problem.
- You want a "before" recording to compare against after a fix.
- A bug report needs evidence that a regression occurred.
Imported profiles open in the same views, but they do not link back to a live page. You can read timings, but you cannot inspect component props the way you can in the Components tab. Name the exported files clearly, for example search-typing-before.json.
Common problems
The Profiler tab is missing or disabled. The page is not using React, the page uses the standard production build, or the extension has not reloaded. Check for a gray DevTools icon and switch to a development or profiling build.
"No profiling data has been recorded." You stopped without a commit happening. Either the interaction did not change anything, or you clicked in a different tab or iframe. Perform an interaction that changes state and record again.
Numbers are far higher than in production. You are recording a development build. Development builds also run Strict Mode checks that render components twice, which inflates times. Compare changes inside the same build.
Timings vary between runs. Browser extensions, background tabs, CPU throttling settings, and thermal limits on a laptop all change results. Close other tabs, repeat the same interaction several times, and compare patterns rather than single numbers.
The first commit is far slower than the rest. The mount does more work than updates. Treat mount and update commits separately.
Habits that make recordings useful
- Change one thing per recording so any difference has one cause.
- Record the same interaction at least three times and compare commit durations.
- Save the baseline before touching any code.
- Note the build type next to each saved file.
Exercise
You recorded a session in a development build and saw a 40 ms commit. A colleague says the production app felt fine. Name two reasons both observations can be true, and say what you would do next.
Show answer
Development builds add extra checks and Strict Mode double renders, so the same work takes longer than in production. Machine load and background tabs can also change timings. Next, record the same interaction in a profiling build (react-dom/profiling), repeat it several times, and compare the commit durations there before deciding whether a fix is needed.
Exercise
Why would you use "Reload and start profiling" instead of the ordinary record button when you want to analyze the initial page load?
Show answer
The initial mount commits before you can click Record. The reload button starts profiling before the page loads again, so the mount commit is captured. The ordinary record button only captures updates that happen after you press it.