Sub topic 5
Reading the flamegraph and ranked charts
This chapter assumes you can build React function components with props, state, and hooks. It also assumes you have recorded a session in the React DevTools Profiler. You will learn to read the two main views of a recording and to pick out the components worth fixing.
What a recording contains
A Profiler recording is a list of commits. Each commit is one time React updated the screen. The bar chart at the top right of the Profiler shows one bar per commit. A taller, warmer bar means that commit took longer. Click a bar to inspect that commit. The views below always show the commit you selected.
The flamegraph
The flamegraph draws your component tree for the selected commit. The root is at the top and children sit below their parents. Each bar is one component.
- Width shows how long the component and its children took to render in this commit.
- Color shows relative cost. Warm colors (yellow to orange) are slow, cool colors (blue to green) are fast.
- Gray bars did not render in this commit. React skipped them.
Gray bars matter as much as colored ones. If you expected a component to stay still and it is colored, it re-rendered. That is your first lead.
Hover over a bar to see its render time. The tooltip shows two numbers: the time spent in the component itself ("self") and the total including children.
The ranked chart
The ranked chart lists the same components sorted by render time, slowest first. It only shows components that rendered in the selected commit. Use it when the tree is large and the flamegraph is hard to scan. The top bar is the component that cost the most, so it is usually the first place to look.
Remember that a parent's time includes its children. A parent can sit at the top of the ranked chart only because one slow child is inside it. Check the child's bar before you blame the parent.
A worked reading
Suppose you type one character in a search box and record the commit. The flamegraph shows this:
Appis orange and wide, as expected for a root.SearchBoxis small and fast.ExpensiveTreeis orange and takes 38 ms, though your typing did not change its data.- A sibling
Footeris gray.
The ranked chart confirms it. ExpensiveTree is at the top. You now know which component to investigate, and the next chapter shows how to find out why it rendered. The gray Footer shows what a skipped render looks like.
Common mistakes
- Reading a development build. Timings in development are inflated and uneven, so compare them only with other development timings.
- Judging by a single commit. Look at several commits of the same interaction before you decide.
- Optimizing a fast component. A bar can be wide in the flamegraph and still be 0.3 ms. Read the milliseconds, not just the width.
Exercise
In a commit, the flamegraph shows List in orange at 45 ms, and the ranked chart shows Row at 40 ms as the top entry. Where is most of the time spent, and why?
Show answer
Most of the time is spent in Row. The 45 ms for List includes its children, so it appears slow largely because of the Row components inside it. Open Row to see its own render time and why it rendered.