Sub topic 10
Recording a profiling session
This chapter covers the Profiler tab in React Developer Tools: how to record a session, what the recording captures, and the mistakes that make a recording useless. It assumes you can write function components, use useState and props, and use the browser's Elements and Console panels. It also assumes you can run a React app locally in development mode.
What a profiling session is
The Components tab shows the app as it is right now. The Profiler tab shows what happened over a span of time. You start recording, interact with the app, and stop. React then reports each commit made during that span.
A commit is the moment React applies the results of a render to the DOM. One click can cause one commit or several. The Profiler groups its data by commit, so the commit is the unit you will navigate.
For each commit the Profiler records:
- Which components rendered.
- How long each component took to render.
- The time spent rendering the commit as a whole.
- When the commit happened, relative to the start of the recording.
- Why each component rendered, if you enable that setting (covered in a later chapter).
Components that did not render in a commit are shown greyed out. That is also useful information, because it shows what React skipped.
Before you record
A recording is only as good as its setup. Check these three things first.
- Use a build that can be profiled. Development builds support profiling, but they are slower than production builds because React does extra checks. Production builds strip profiling support unless you build with a profiling flag. The previous chapter on build modes covers this. For a first pass, a development build is fine for finding which components render. Treat its timings as relative, not absolute.
- Open the Profiler tab on the right page. If the extension icon is grey or the tab is missing, React was not detected on the page.
- Know what you want to measure. Decide on one interaction, such as clicking an item in a list. A recording of several unrelated actions is hard to read.
Recording step by step
The example uses the first code sample from the plan: a memoized Item inside a List. Its onSelect prop is a new function on every render.
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>
);
}Render <List items={['Apple', 'Banana', 'Cherry', 'Date']} /> in a development build, then follow these steps.
- Open the browser developer tools and choose the Profiler tab.
- Open the Profiler settings (the gear icon). Turn on Record why each component rendered while profiling. This adds some overhead, but you need it to answer "why did this render?" later.
- Click the blue Start profiling button (a filled circle). The button turns red while recording.
- Switch to the page and click one item, for example "Banana". Do one thing only.
- Return to the DevTools and click Stop profiling.
The Profiler now shows the result of your recording.
The recording workflow as a sequence
The pieces involved are the page, React, and the Profiler. React reports to DevTools only while recording is on.
Reading the first result
After you stop, the Profiler shows a bar chart at the top right. Each bar is one commit. In the example there is one bar. A taller, more yellow or orange bar means a slower commit. A blue or green bar means a fast one. A greyed bar means the commit happened but nothing in your tree was measured as rendering.
Below the bar chart is the chart for the selected commit. By default this is the flamegraph. The next chapter explains how to read the flamegraph and ranked charts in detail. For now, look at three things:
- The component names that are colored. These rendered.
- The component names that are grey. These did not.
- The time shown next to each name.
In the example, the click changes state in List. List renders, and so do all four Item components. Each Item is wrapped in memo, so you might expect them to be grey. They are not, because onSelect is a new function on every render, and memo sees a changed prop. This is the kind of finding the Profiler exists to surface.
Navigating commits
With several commits in a recording, you move between them using the bar chart or the controls above it.
- Click a bar to select that commit.
- Use the left and right arrow buttons to step to the previous or next commit.
- The number beside the arrows shows which commit you are on and how many were recorded, for example "1 / 3".
The Profiler also has a Filter commits option in its settings. It hides commits shorter than a threshold you set. This is helpful when a recording has many tiny commits and you want to see the expensive ones.
Settings that change what you see
| Setting | What it does | When to use it |
|---|---|---|
| Record why each component rendered while profiling | Stores the reason for each render, such as changed props or changed state | Turn on whenever you are hunting extra renders |
| Hide commits below a threshold | Removes short commits from the bar chart | Turn on when many tiny commits obscure a slow one |
| Reload and start profiling (the circular arrow button) | Reloads the page and begins recording at once | Use to profile the initial mount |
The reload button matters because the first render happens before you can click anything. Without it, you cannot record the mount.
Recording the initial mount
To profile what happens when the app first loads:
- Open the Profiler tab.
- Click Reload and start profiling.
- Wait until the app finishes loading.
- Click Stop profiling.
You will see commits for the initial render and any follow-up renders, for example after data loads and state updates. Mount recordings usually show larger commits than interaction recordings, because every component renders for the first time.
Common errors
The Profiler tab is missing or empty. React was not detected, or the page uses a production build without profiling support. Check that the extension is enabled for the site and that you are running a development build.
"No profiling data has been recorded." You stopped without any commit occurring. Start again and make sure your interaction actually triggers a state or prop change. If you click something that does nothing, nothing is recorded.
Timings look wrong or inconsistent. Development builds add overhead, and browser extensions and other tabs add noise. Run the same interaction a few times and compare. Absolute numbers from a development build do not predict production speed. The relative differences between components and between before and after a fix are still meaningful.
Recording an interaction that starts before you press Start. If you click first and then start recording, the render has already happened. Always start recording first.
Recording for too long. A long recording with many interactions buries the signal. Record one interaction at a time. You can run several short sessions.
No render reasons. If you did not enable the render-reason setting before recording, reasons are missing. You cannot add them afterward. Turn on the setting and record again.
Saving and sharing a recording
The Profiler has buttons to export a recording to a JSON file and to import one. This lets you hand a recording to a teammate, or compare a recording before and after a fix. Imported data opens in the Profiler as a normal session, even if the app is not running.
A practical habit: export the "before" recording, make a change, record again, and compare the two. You can then show that the number of rendered components went down, instead of relying on memory.
A short checklist
- Start from a development build, or a profiling build if you need realistic timings.
- Enable the render-reason setting.
- Click Start profiling (or Reload and start profiling for a mount).
- Perform one interaction.
- Click Stop profiling.
- Check how many commits you got and which one to inspect.
- Export the recording if you want to compare it later.
Exercise
You recorded a session, then noticed that none of the commits list a reason for why components rendered. What went wrong, and what should you do?
Show answer
The setting "Record why each component rendered while profiling" was off when you recorded. Render reasons are captured only during recording and cannot be added afterward. Turn the setting on in the Profiler settings and record the interaction again.
Exercise
You want to find out why your app is slow to appear on first load. Clicking "Start profiling" and then reloading the browser tab does not produce a useful recording. Which button should you use instead, and why?
Show answer
Use "Reload and start profiling". It reloads the page and begins recording immediately, so the initial mount is captured. If you start recording and reload by hand, the connection to DevTools can be reset or the first renders can be missed.
Exercise
In the memoized list example, you record one click on "Banana". All four Item components show as rendered, even though they are wrapped in memo. Which prop is the likely cause, and what would you check next in the Profiler?
Show answer
The onSelect prop. The inline arrow function is a new object on each render of List, so memo sees a changed prop. Next, select one Item in the recorded commit and read its render reason, which should list onSelect as a changed prop.