Sub topic 11
Reading the flamegraph and ranked charts
This chapter assumes you can write React function components, use useState and props, and have already recorded a session in the Profiler tab. If you haven't, finish the recording chapter first. Here you will learn to read the two main charts the Profiler draws for each commit: the flamegraph and the ranked chart. You will also learn which numbers to trust and which to ignore.
What a commit is and why charts are per commit
A render pass in React has two phases. In the render phase, React calls your components to work out what the UI should look like. In the commit phase, React applies the result to the DOM. The Profiler groups its data by commit. Each commit is one time React wrote changes to the screen.
When you stop a recording, the Profiler shows a bar chart near the top right with one bar per commit. The height and color of a bar show how long that commit took to render. Taller, warmer-colored bars (yellow to orange) are slower, and short blue-green bars are fast. Click a bar to select that commit. The flamegraph and ranked charts then redraw for the selected commit only.
You can step through commits with the arrow buttons next to the bar chart. Always check which commit is selected before you draw conclusions from a chart.
The flamegraph
The flamegraph mirrors the component tree. The root component is at the top. Its children sit in a row below it, and their children below those. Each rectangle is one component. A wide rectangle took more time than a narrow one.
What the width means
The width of a rectangle shows how long that component and its descendants took to render in this commit. This is called its inclusive time. It is not the time spent in the component's own code. A component with a wide bar might be cheap itself and simply have an expensive child.
The Profiler shows the self time in the sidebar when you select a rectangle. Self time is the time spent in that component's own function, excluding children. Compare the two numbers. If the inclusive time is large and the self time is small, look at the children. If both are large, the component itself is the cost.
What the colors mean
Colors in the flamegraph carry information:
| Color | Meaning |
|---|---|
| Warm (yellow to orange) | The component took a relatively large share of the commit's time |
| Cool (blue to green) | The component took a smaller share of the time |
| Gray | The component did not render in this commit |
The gray case matters most. A gray rectangle means React reused the previous output and did not call that component. This is what memo or an unchanged subtree looks like when it works. If you expect a component to be skipped and it is colored, it rendered.
The color scale is relative to the selected commit. A component that is orange in a 2 ms commit is not slow in absolute terms. Read the milliseconds, not only the color.
Reading the tree shape
A flamegraph is easy to misread if you look only at the widest bar. Use these habits instead:
- Find the top of the tree that is colored, not gray. That is where rendering started.
- Follow the colored path downward. Every component on it rendered.
- Look for where gray begins. Components below that point were skipped.
- Select a few rectangles and read the sidebar for time and render reasons.
If the whole tree is colored after a small state change in one leaf component, the state probably lives too high in the tree. Everything below the owner of the state re-renders by default.
The ranked chart
The ranked chart answers a different question: which components cost the most in this commit? It drops the tree structure. It lists only the components that rendered, sorted from the longest to the shortest, with the longest bar at the top.
Each bar uses the same inclusive width idea as the flamegraph. A parent that contains an expensive child therefore also ranks high. The ranked chart is a fast way to find candidates, and the flamegraph helps you decide whether a candidate is the cause or only a container.
| Question | Better chart |
|---|---|
| Where in the tree did rendering start? | Flamegraph |
| Which components were skipped? | Flamegraph |
| Which single component cost the most? | Ranked chart |
| Is the cost in a component or in its children? | Flamegraph, then the sidebar |
| Is the commit dominated by one item or spread out? | Ranked chart |
The ranked chart omits components that did not render. If a component is missing from it, it was not part of that commit. Selecting a bar in the ranked chart also selects it in the flamegraph and the sidebar, so you can move between the two views.
Reading the sidebar for one component
Select any rectangle and look at the right-hand side of the Profiler. For a component that rendered, it shows:
- The render duration of the component in this commit, along with the time of the commit.
- The reason it rendered, if the setting for recording why each component rendered was on during recording.
- A list of commits in which this component rendered, with the times, so you can jump to them.
The "why did this render" information needs the profiler setting to be enabled before you record. If you open an older recording that was made without it, the sidebar shows no reasons. Turn the setting on and record again. The next chapter covers render reasons in detail.
A worked example
Use the unoptimized list from the Topic Spine as a test case. Every click on an item changes selected, which lives in List. The inline onSelect function is created fresh in each render, so memo on Item cannot skip anything.
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>
);
}Record a session, click one item, and stop recording. Select the commit caused by the click.
In the flamegraph you will see List at the top, and Item rectangles in a row below it. All of them are colored, even though only selected changed. If memo were working, the Item rectangles would be gray. Each Item is narrow, because each one does very little work. The List bar is wide because its inclusive time covers every item.
In the ranked chart, List appears first. The Item bars follow, all about the same length. This pattern, many equal small bars below a parent, tells you the cost is the number of renders and not any single slow component. The fix is to stop the renders, not to make Item faster.
Now apply the useCallback version.
import { memo, useCallback, 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);
const handleSelect = useCallback((value) => setSelected(value), []);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item key={label} label={label} onSelect={handleSelect} />
))}
</ul>
</div>
);
}Record the same click again. In the flamegraph, List is still colored, because its state changed. The Item rectangles are now gray, and the ranked chart lists only List. The commit is shorter, and the charts show why.
Common mistakes when reading the charts
- Reading a development build as a measurement. Development builds are slower and add extra checks. Use the numbers to compare components inside one recording, not as production timings. The build modes chapter covers this.
- Comparing colors across commits. The color scale is set per commit. Compare milliseconds instead.
- Blaming the widest bar. Inclusive time includes children. Check self time before you edit a component.
- Looking at the wrong commit. After a recording, the Profiler may select the first commit, which is often the initial mount. Click the commit that matches your interaction.
- Treating gray as an error. Gray means skipped, which is usually the goal.
- Ignoring commit count. Fifty fast commits for one user action can hurt more than one slow commit. The bar chart at the top shows this.
Exercises
Exercise
In the flamegraph for a commit, a parent component has a wide, orange bar. Its self time in the sidebar is 0.2 ms, and its inclusive time is 38 ms. Where should you look for the cost, and why?
Show answer
Look at its children. The inclusive time covers the component and everything below it, while the self time covers only its own function. With 0.2 ms of self time, the parent is cheap, and the other 37.8 ms is spent in descendants. Select the child rectangles and compare their self times to find the expensive one.
Exercise
After clicking one item in the unoptimized list, you see all Item rectangles colored in the flamegraph. After adding useCallback, you see them gray. What does each result tell you about memo?
Show answer
Colored rectangles mean Item rendered, so memo did not skip it. The inline onSelect was a new function on every render, so the props comparison failed. Gray rectangles mean React reused the previous output. The stable handleSelect let the props comparison pass, so memo skipped the render.
Exercise
Why is it a mistake to treat the color of a rectangle as an absolute measure of slowness?
Show answer
The color scale is relative to the selected commit. The slowest component in a very fast commit can be orange even though it took only a few milliseconds. Read the time in milliseconds in the sidebar, and compare components inside the same commit.