Sub topic 5
Reading the flamegraph and ranked charts
This chapter assumes you can build React function components with state and hooks. It also assumes you have recorded a session in the React DevTools Profiler tab, which the previous chapter covered. Here you learn to interpret what the recording shows and decide where to look next.
What a recording contains
A Profiler recording is a list of commits. A commit is one point where React applied an update to the DOM. For each commit, DevTools stores which components rendered and how long each took. You always look at one commit at a time. The two main views, the flamegraph and the ranked chart, are two drawings of the same selected commit.
At the top right of the Profiler, a bar chart shows one bar per commit. Taller and warmer-colored bars are commits that took longer. Click a bar, or use the arrow buttons, to select that commit. The selected bar is highlighted, and the chart below changes to match it.
The flamegraph
The flamegraph shows the component tree for the selected commit. The root is at the top and children sit below their parents. Each bar is one component.
- Width is how long that component and all of its descendants took to render. A wide parent can simply have many children, so width alone does not make a component slow.
- Color shows relative cost within the commit. Cool colors (blue and green) are cheap. Warm colors (yellow and orange) are expensive.
- Gray means the component did not render in this commit. React reused its previous output.
Hover over a bar to see a tooltip. It gives the component name, its own render time, and the total time including children. Click a bar to select it. The side panel then shows details such as the props, state, and why it rendered (the next chapter covers that). Clicking also zooms the view to that subtree, which helps in deep trees.
The most useful thing about the flamegraph is its gray areas. If you changed one input and a whole section of the tree is colored, that section re-rendered. Compare it with what you expected. A large colored region around data that did not change is the usual sign of unnecessary re-renders.
Self time and total time
Every bar has two numbers, and mixing them up leads to wrong fixes.
| Term | Meaning | Use it to |
|---|---|---|
| Self time | Time spent in this component's own function body | Find expensive code in that component |
| Total time | Self time plus all descendants | Find expensive subtrees |
A page root often has a large total time and a tiny self time. Optimizing the root would be pointless. The cost is in a descendant. The reverse also happens: a leaf with a wide bar and a high self time is doing expensive work itself, such as sorting a large array during render.
The ranked chart
The ranked chart lists the components that rendered in the selected commit, sorted so the most expensive is at the top. Each bar's width is scaled to the top item. Components that did not render do not appear at all.
Use it when the flamegraph is too large to scan. In a tree with hundreds of nodes, the ranked chart shows the few worst components at once. It loses the tree structure, though. You cannot see who the parent is, or whether a slow component is a child of something that should have stopped the update. Click a bar in the ranked chart, then switch to the flamegraph. The selection carries over, so you can see where that component sits.
A practice component
Reading charts is easier with a known problem. This component renders 200 rows, and each row does deliberately slow work. Put it in a Vite or Create React App project, run it in development, and record while you type in the input.
import { useState } from "react";
function slowSquare(n) {
// Busy loop to simulate expensive render work.
const end = performance.now() + 0.05;
while (performance.now() < end) {}
return n * n;
}
function Row({ n }) {
return <li>{n} squared is {slowSquare(n)}</li>;
}
function Rows() {
const numbers = Array.from({ length: 200 }, (_, i) => i);
return (
<ul>
{numbers.map((n) => (
<Row key={n} n={n} />
))}
</ul>
);
}
export default function App() {
const [text, setText] = useState("");
return (
<div>
<input value={text} onChange={(e) => setText(e.target.value)} />
<Rows />
</div>
);
}Each Row spends about 0.05 ms in its own function, so Rows totals around 10 ms or more per keystroke. Record, type three characters, and stop. You should see three tall commits. In the ranked chart, the Row entries fill the top. In the flamegraph, App and Rows are wide, with 200 thin Row children below.
Notice what the charts tell you. No single Row is slow, so ranked order points at a repeating pattern, not at one culprit. And Rows re-rendered although text is irrelevant to it. That second finding is the real problem, and fixing it is the topic of the later state-colocation chapter.
Patterns to recognize
Several shapes recur in real recordings.
- One wide, warm leaf. A single component with high self time. Look at its function body for sorting, filtering, parsing, or large loops.
- Many thin, equal bars. A list where each item is cheap but there are many. The cost is the count. Memoizing the items or virtualizing the list fits here.
- A colored region unrelated to the change. A parent state update dragged a whole subtree along. The cause is structural, not computational.
- Alternating gray and colored siblings. Memoization is working for some children and failing for others. Check which props change identity.
- Many tiny commits in a row. Often a loop of state updates from effects or events. One commit is cheap, but the sum is not.
Pitfalls when reading timings
A few more edge cases to keep in mind:
- Hover times are rounded. A component showing 0 ms may still render. Do not read zero as "free" when it renders hundreds of times.
- Colors are relative to the commit. A yellow bar in a 2 ms commit is not a problem. Check the commit duration before worrying about a warm color.
- Gray does not mean absent. A gray bar was skipped this commit. Its width shows the time it took the last time it rendered.
- Only React work appears. Time spent in layout, painting, network, or non-React JavaScript is invisible here. If the commit bar is short but the page still feels slow, the bottleneck is elsewhere, and the browser Performance panel chapter covers that.
- Recordings include your profiling overhead. Compare commits within one recording, and avoid comparing numbers between a profiled run and an unprofiled one.
Summary of the two views
| Question | Best view |
|---|---|
| Which commit was slowest? | Commit bar |
| Which components cost the most in this commit? | Ranked chart |
| Where do they sit in the tree, and what did not render? | Flamegraph |
| Is the cost inside a component or in its children? | Tooltip self and total times |
Exercise
You record one keystroke in a form. The flamegraph shows Page as the widest bar but its self time is 0.2 ms. The ranked chart's top entry is PriceTable with a self time of 14 ms, and Sidebar is colored even though the keystroke only changed a field in Form. Which two findings should you act on, and what does each suggest?
Show answer
First, PriceTable has a high self time, so look for expensive work in its own render body, such as sorting or filtering that could be cached with useMemo. Second, Sidebar rendered although it is unrelated to the change, so investigate why it re-rendered. The likely fix is moving state down or memoizing it. Page is wide only because it contains the other two, so optimizing it directly would not help.
Exercise
In the practice component, the ranked chart shows 200 Row bars of similar width, and no single bar stands out. Why is this not evidence that Row is the thing to optimize first, and which view would you use to find the better fix?
Show answer
Each Row is cheap on its own, and the cost comes from the whole list re-rendering on every keystroke even though its data did not change. The flamegraph shows that Rows is rendered as a sibling of the input under the state-owning App. The better fix is structural: move the text state into its own component so Rows does not re-render at all.