Sub topic 13
Going beyond React: the browser Performance panel
This chapter assumes you can build React function components with hooks, that you have used the React DevTools Profiler from earlier chapters, and that you have opened the browser developer tools before. The examples use Chrome DevTools. Edge uses the same panel, and Firefox has a similar Performance tool with different labels.
Why the React Profiler is not enough
The React DevTools Profiler measures one thing: the time React spends rendering components and committing changes. A slow page can be slow for reasons that never appear there:
- A long JavaScript task that has nothing to do with rendering, such as parsing a large JSON response.
- A large JavaScript bundle that takes seconds to download, parse, and run before React starts.
- Layout and paint work the browser does after React commits to the DOM.
- Network waits for data or scripts.
The browser Performance panel records everything the main thread does, whether or not React is involved. Use the React Profiler to answer "which component is slow?" Use the Performance panel to answer "where does the whole page spend its time?"
The main thread and long tasks
A browser runs JavaScript, handles clicks, calculates layout, and paints on one main thread. Work on this thread is grouped into tasks. While a task runs, the browser cannot respond to input.
A long task is a task that runs for more than 50 ms. Long tasks are the usual cause of a page that feels janky. A click that arrives during a 300 ms task waits up to 300 ms before its handler even starts.
The React Profiler sees only steps C and D. The Performance panel shows all of them.
Recording a trace
- Open your app in a production-like build if you can. Development builds add warnings and extra checks that distort timings, as covered earlier in this tutorial.
- Open DevTools and select the Performance tab.
- Consider enabling CPU throttling (for example 4x slowdown) from the settings gear. Developer machines are faster than many users' devices, and throttling makes problems easier to see.
- For an interaction, click Record, perform the action (type in a box, open a menu), then click Stop. Keep recordings short, ideally under 10 seconds.
- For page load, use the "Record and reload" button instead.
Close or disable browser extensions first, or test in an incognito window. Extensions add their own tasks to the trace.
Reading the trace
The recording has several areas, from top to bottom.
| Area | What it shows | What to look for |
|---|---|---|
| Overview (top strip) | CPU activity, frames, and network over time | Tall colored blocks mean busy periods. Select a range by dragging. |
| Frames | Each rendered frame and its duration | Frames marked in red or taking much longer than 16 ms |
| Interactions | Clicks, key presses, and their timing | Interactions that take longer than about 200 ms |
| Main | A flame chart of everything the main thread ran | Wide blocks with a red triangle in the corner (long tasks) |
| Network | Requests as bars on a timeline | Large scripts that load late or block other work |
| Bottom-Up / Call Tree / Event Log tabs | Summaries of the selected range | Which functions took the most self time |
In the Main flame chart, the horizontal axis is time and the vertical axis is the call stack. A wide block is a function that took a long time. The blocks below it are the functions it called. Colors group the work by type:
- Yellow is JavaScript execution.
- Purple is style recalculation and layout.
- Green is paint and compositing.
If a task is mostly yellow, JavaScript is the cost. If it has large purple sections, the browser is spending time on layout, often because the DOM is large or the code forces layout repeatedly.
Finding React inside the trace
React's own work appears in the Main chart as ordinary JavaScript function names. In a production build, names are often minified. In a development build you will see names such as performConcurrentWorkOnRoot, renderWithHooks, and your component names. The part of the stack named commitRoot and its children is the commit phase.
Two practical steps help:
- Select a long task in the Main chart. The Summary tab at the bottom shows the total time split into scripting, rendering, painting, and idle.
- Open Bottom-Up and sort by Self Time. Self time counts only time spent in a function itself, not in the functions it called. The function with the most self time is where the CPU actually spent its effort.
If the biggest self-time entries are your own component functions, go back to the React Profiler and the fixes from earlier chapters. If the biggest entries are something else, such as JSON.parse, a sort function, a library call, or Recalculate Style, then memoizing components will not help.
Example: a slow handler that React cannot see
The component below parses and sorts a large dataset inside a click handler. The work happens before React renders anything, so the React Profiler shows a fast render. The Performance panel shows a long task.
import { useState } from "react";
function buildRows(count) {
const rows = [];
for (let i = 0; i < count; i++) {
rows.push({ id: i, name: `Item ${i}`, score: Math.sin(i) * 1000 });
}
return rows;
}
export function Leaderboard() {
const [rows, setRows] = useState([]);
function handleLoad() {
// Simulates a large API payload that must be parsed and sorted on the main thread.
const json = JSON.stringify(buildRows(300000));
const parsed = JSON.parse(json);
parsed.sort((a, b) => b.score - a.score);
setRows(parsed.slice(0, 50));
}
return (
<>
<button onClick={handleLoad}>Load leaderboard</button>
<ul>
{rows.map((row) => (
<li key={row.id}>
{row.name}: {row.score.toFixed(1)}
</li>
))}
</ul>
</>
);
}Record a trace while clicking the button. In the Main chart you will see one wide yellow task under the click event. Inside it, JSON.stringify, JSON.parse, and sort take nearly all the time. The React render at the end is a thin sliver, because only 50 rows are drawn.
The fix is outside React: do less work on the main thread. Options include asking the server for sorted and paginated data, or moving the parsing into a Web Worker. Optimizing the 50-row component would change nothing.
Bundle size and load cost
Page load problems usually show up in two places in the trace.
- Network track: large script files appear as long bars. A script that takes 2 seconds to download delays everything that depends on it.
- Main track after download: a long yellow block labeled "Evaluate Script" or "Compile Code" is the browser parsing and running the bundle. Parse and execute cost grows with the amount of JavaScript, even when it is cached.
To see what is in your bundle, use a bundle analyzer for your build tool (for example webpack-bundle-analyzer, or rollup-plugin-visualizer for Vite). It draws a treemap where each rectangle's area is the file's size. Look for large libraries that only one screen uses. Those are candidates for the code splitting technique shown with React.lazy in the next chapter.
The Coverage tool (DevTools menu, More tools, Coverage) shows how much of each downloaded script ran during your recording. A bundle that is 80% unused on first load is a sign that code splitting would pay off.
Common errors when profiling
- Profiling a development build and trusting the numbers. React in development mode does extra work, so absolute times are inflated. Use relative comparisons or a production build.
- Recording too much. A 60-second trace is hard to read. Record one interaction at a time.
- Comparing runs with different conditions. Compare traces with the same throttling, the same data, and the same cache state. A cold load and a warm load differ a lot.
- Optimizing the widest block you can name. Check self time first. A wide block may just be a parent calling something expensive.
- Ignoring the network. If the slow part is a request that returns late, no rendering fix will help.
- Forgetting extensions. Browser extensions can inject long tasks into the trace.
A short workflow
- Reproduce the slow interaction and record only that interaction.
- Find the longest task in the Main chart and read its Summary breakdown.
- Open Bottom-Up, sort by self time, and identify the dominant function.
- Decide which tool fits. Component re-renders go to the React Profiler. Heavy computation goes to a worker, a server change, or less work. Layout cost goes to a smaller DOM. Download and parse cost go to code splitting.
- Change one thing, record again under the same conditions, and compare.
Exercise
You click a button and the page freezes for about 400 ms. The React DevTools Profiler shows the component update took 6 ms. In a Performance panel trace, what do you look for to find the cause, and why does adding React.memo probably not help?
Show answer
Select the long task under the click event in the Main chart, read the Summary breakdown, and open Bottom-Up sorted by self time to find the function that used most of the time (for example JSON.parse, a sort, or a layout recalculation). React.memo only skips component re-renders, and the React Profiler already shows rendering takes just 6 ms. The other roughly 394 ms is spent outside React rendering, so memoizing components cannot reduce it.
Exercise
A trace of page load shows a 1.8 second bar in the Network track for main.js, followed by a 700 ms "Evaluate Script" block on the main thread. Name two tools or techniques from this chapter that help you decide what to remove or defer, and say what each tells you.
Show answer
A bundle analyzer (such as webpack-bundle-analyzer or rollup-plugin-visualizer) shows which modules take up the most space in the bundle, so you can find large libraries used by only one screen. The Coverage tool shows how much of main.js actually ran during the recording, so you can see how much code is unused at load. Both point to code splitting with React.lazy as the fix.