Sub topic 12
Measuring in Code with the Profiler Component
This chapter assumes you can build React function components with props, state, and hooks. You should also know the render and commit phases and the difference between development and production builds, covered in earlier chapters.
The DevTools Profiler is good for exploring. Sometimes you need numbers inside your own code, for example in a test, a staging build, or an analytics call. React provides the Profiler component for this.
How the Profiler component works
Profiler wraps part of your tree. Each time a component inside it commits an update, React calls the onRender function you provide. It requires two props:
id: a string that names the part of the tree you are measuring.onRender: a callback that receives the timing data.
import { Profiler } from "react";
function onRender(id, phase, actualDuration, baseDuration, startTime, commitTime) {
// phase is "mount", "update", or "nested-update"
console.log(`${id} ${phase}: ${actualDuration.toFixed(1)}ms (base ${baseDuration.toFixed(1)}ms)`);
}
export default function App() {
return (
<Profiler id="Dashboard" onRender={onRender}>
<Dashboard />
</Profiler>
);
}What the arguments mean
| Argument | Meaning |
|---|---|
| `id` | The `id` prop of the `Profiler` that reported. |
| `phase` | `"mount"` for the first render, `"update"` for a re-render, `"nested-update"` for an update caused inside a layout effect or similar. |
| `actualDuration` | Milliseconds spent rendering the subtree for this commit. |
| `baseDuration` | Estimated milliseconds to render the whole subtree with no memoization. |
| `startTime` | When React began rendering this update. |
| `commitTime` | When React committed this update. |
Compare the two durations. If actualDuration is much smaller than baseDuration, memoization is skipping work. If the two are close on every update, little is being skipped, and the subtree may be re-rendering more than it needs to.
Using it in practice
You can nest several Profiler components with different ids. This tells you which region of the page is costly. Put one around a whole page and others around its heavy sections.
<Profiler id="Page" onRender={onRender}>
<Header />
<Profiler id="Table" onRender={onRender}>
<BigTable />
</Profiler>
</Profiler>Keep the callback cheap. It runs on every commit in that subtree, so heavy work inside it adds to the cost you are measuring. A common approach is to push records into an array and report them in batches.
Common errors
- Reading numbers from a development build. Development builds are slower and add extra checks, so timings are inflated. Judge real cost from a production build with profiling enabled.
- Expecting a call with no commit.
onRenderruns only when something in the subtree commits. If a render is skipped, there is no call. - Leaving it everywhere. Profiling adds overhead, and standard production builds disable the
Profilercomponent, so it reports nothing there. Use a profiling build if you need production-like data, or remove the wrappers when you finish. - Judging from one sample. A single
actualDurationis noisy. Record several updates and compare the typical values.
Exercise
A Profiler reports actualDuration of 2 ms and baseDuration of 40 ms for an update. What does this tell you?
Show answer
Memoization is working. React skipped most of the subtree, so only about 2 ms of work happened, while rendering everything without memoization would take about 40 ms.
Exercise
Why might onRender never be called in your deployed site, even though it logs in development?
Show answer
Standard production builds disable the Profiler component, so the callback does not fire. Use a profiling build to measure in production-like conditions.