日本語

Measuring React Render Time with the Profiler Component

​​React​esbuild,memo,TypeScript,useLayoutEffect

This article was translated from the Japanese original with AI. 日本語の原文を読む

Wrapping part of a page in React's <Profiler> calls onRender each time that part finishes rendering, and gives you how long the render took. Because the numbers can be logged, you can check whether memo is working and compare which parts are slow.

In a test environment, I wrapped a component that renders a 1,000-row list in <Profiler> and ran it in Chromium. I checked how to read the values, how they change when memo is used, how onRender is called when several <Profiler> components are placed, and how the production build behaves.

Note

The test environment was Ubuntu 24.04 and Node.js 22.22.0. I used react and react-dom 19.3.1, @types/react 19.3.0, typescript 7.0.2, and esbuild 0.28.2. I drove Chromium 141 with Playwright 1.56.1. I type-checked the .tsx files with tsc, then converted them with esbuild into three builds: development, production, and profiling.

Wrapping in Profiler to receive onRender

<Profiler> takes two props: id, which names the measured part, and onRender, the callback (Props in the official reference).

In the test, onRender pushed the values it received into an array, which I later read out from Playwright. For the type, I used ProfilerOnRenderCallback from @types/react.

ts
// log.ts
import type { ProfilerOnRenderCallback } from 'react';

export type Entry = {
  id: string;
  phase: string;
  actualDuration: number;
  baseDuration: number;
  startTime: number;
  commitTime: number;
};

declare global {
  interface Window { __entries: Entry[] }
}
window.__entries = [];

export const onRender: ProfilerOnRenderCallback = (
  id, phase, actualDuration, baseDuration, startTime, commitTime,
) => {
  window.__entries.push({ id, phase, actualDuration, baseDuration, startTime, commitTime });
};

The component being measured is shown below. Pressing the button increments a number, and each press also re-renders SlowList. SlowList renders 1,000 Row components, and each row does a small amount of computation.

tsx
function Basic() {
  const [count, setCount] = useState(0);
  return (
    <Profiler id="App" onRender={onRender}>
      <button id="inc" onClick={() => setCount((c) => c + 1)}>+1</button>
      <p>{count}</p>
      <SlowList count={ROWS} />
    </Profiler>
  );
}

The six values onRender receives

onRender receives these six values, in this order. The descriptions are based on the arguments of onRender in the official reference. Times are in milliseconds.

Argument Meaning
id The id of the <Profiler> whose render was committed
phase "mount" for the first display, "update" for a render caused by a change in props, state, and so on. There is also "nested-update"
actualDuration Time spent rendering the <Profiler> and its components in this update
baseDuration An estimate of how long it would take to re-render the whole subtree without any optimization. It is the sum of the most recent render times of each component
startTime The time when React started rendering this update
commitTime The time when React committed this update. All <Profiler> components whose onRender is called in one commit receive the same value

In the types for @types/react 19.3.0, phase is one of "mount" | "update" | "nested-update". id is string, and the other four values are number.

Looking inside the profiling build, I found that times are measured with unstable_now from the Scheduler. In the Scheduler's production code, this calls performance.now(). In the Chromium test environment, every value was in steps of 0.1 ms. The test page was not cross-origin isolated (crossOriginIsolated was false). The MDN page for performance.now() says that on pages that are not cross-origin isolated, the precision is reduced to 100 microseconds. So a log entry showing 0.00 does not necessarily mean the render took zero time.

Checking whether memo works with actualDuration and baseDuration

The baseDuration description in the official reference says that comparing actualDuration with baseDuration shows whether memoization is working.

Here are the results from the profiling build, after pressing the Basic button three times above. The mount line is the first display, and the three update lines are the updates after each button press.

text
--- build=profiling case=basic call count=4
App      mount         actual=  29.30 base=  14.20 start=   95.00 commit=  127.10
App      update        actual=  12.60 base=  12.20 start=  273.10 commit=  288.90
App      update        actual=   8.90 base=   7.80 start=  415.80 commit=  427.30
App      update        actual=   8.20 base=   8.50 start=  566.60 commit=  576.20

Pressing the button only increments a number, but each time the 1,000-row list is re-rendered too. So actualDuration came out about the same as baseDuration.

Next, I wrapped SlowList in memo and pressed the button three times again. The count passed to SlowList does not change, so React should be able to skip rendering it.

tsx
export const MemoSlowList = memo(SlowList);
text
--- build=profiling case=memo call count=4
App      mount         actual=  20.20 base=  13.90 start=   78.00 commit=  100.20
App      update        actual=   0.20 base=  13.90 start=  229.30 commit=  229.70
App      update        actual=   0.00 base=  13.70 start=  357.60 commit=  357.70
App      update        actual=   0.10 base=  13.80 start=  490.70 commit=  490.80

During updates, actualDuration dropped to 0.2 ms or less. baseDuration stayed at about 14 ms. baseDuration is an estimate based on the most recent render times, so it still includes the time for SlowList, even though that render was skipped.

Graph of render times for the memo-wrapped list. On the first display, actualDuration is 20.2 ms and baseDuration is 13.9 ms. On the first to third updates, actualDuration is 0.2, 0.0, and 0.1 ms, while baseDuration stays between 13.7 and 13.9 ms
actualDuration and baseDuration for the memo-wrapped list (profiling build, Chromium 141)
Point

If actualDuration is close to baseDuration on every update, the whole subtree is being re-rendered each time. If only actualDuration drops a lot, rendering is being skipped, for example by memo.

In the test, actualDuration on the first display (mount) was larger than baseDuration. This happened with and without memo, on the second run as well, and in the development build. I have not checked why.

How onRender is called when several Profilers are placed

You can place several <Profiler> components in different parts of the screen, and you can nest them (the official example of measuring different parts of an application). In the test, I placed them like this:

tsx
function Multi() {
  return (
    <>
      <Profiler id="Sidebar" onRender={onRender}>
        <SlowList count={100} />
      </Profiler>
      <Profiler id="Content" onRender={onRender}>
        <Profiler id="Editor" onRender={onRender}>
          <Editor />
        </Profiler>
        <SlowList count={100} />
      </Profiler>
    </>
  );
}

Editor is a component that adds one character each time a button is pressed. I pressed this button twice.

text
--- build=profiling case=multi call count=7
Sidebar  mount         actual=   4.90 base=   2.40 start=   75.50 commit=   83.70
Editor   mount         actual=   0.40 base=   0.10 start=   81.10 commit=   83.70
Content  mount         actual=   2.00 base=   1.40 start=   81.10 commit=   83.70
Editor   update        actual=   0.30 base=   0.30 start=  219.30 commit=  219.70
Content  update        actual=   0.30 base=   1.60 start=  219.30 commit=  219.70
Editor   update        actual=   0.10 base=   0.10 start=  349.40 commit=  349.50
Content  update        actual=   0.10 base=   1.40 start=  349.40 commit=  349.50

On the first display, onRender was called for all three <Profiler> components. After a button press, only Editor and Content, which contains it, were called. Sidebar, which re-rendered nothing inside, did not get an onRender call.

After a button press, Content's actualDuration was the same as Editor's. I think this is because the SlowList inside Content was not re-rendered. However, Content's baseDuration was 1.4 to 1.6 ms, which includes the SlowList time. Also, within one commit, every onRender call had the same commitTime. When collecting logs, you can group entries by commitTime to get one group per commit.

When phase is nested-update

I also checked the case where the third value of phase, "nested-update", appears. The component updates state inside useLayoutEffect.

tsx
function Nested() {
  const [count, setCount] = useState(0);
  const [width, setWidth] = useState(0);
  useLayoutEffect(() => {
    setWidth(count * 10);
  }, [count]);
  return (
    <Profiler id="App" onRender={onRender}>
      <button id="inc" onClick={() => setCount((c) => c + 1)}>+1</button>
      <p>{count} / {width}</p>
    </Profiler>
  );
}

One button press called onRender twice. The first call was "update". The second was a render caused by the update inside the effect, and its phase was "nested-update".

text
--- build=profiling case=nested call count=3
App      mount         actual=   1.60 base=   0.40 start=   71.30 commit=   73.20
App      update        actual=   0.10 base=   0.10 start=  208.20 commit=  208.40
App      nested-update actual=   0.00 base=   0.00 start=  209.10 commit=  209.10

One action resulted in two committed renders. To see the total time for one action, add the two values together. The official reference does not explain in detail when "nested-update" happens. I only checked the case of updating state inside useLayoutEffect.

onRender is not called in production builds

The official reference says that measuring adds a small cost, so profiling is disabled by default in production builds.

When I ran the same four cases in a production build (converted with NODE_ENV set to production), onRender was never called.

text
--- build=prod case=basic call count=0
--- build=prod case=memo call count=0
--- build=prod case=multi call count=0
--- build=prod case=nested call count=0

If you want to measure under conditions closer to production, use a profiling build. The official guide on using profiling builds says to use react-dom/profiling instead of react-dom/client, and that the recommended way to do this is to replace it with a bundler alias.

In the test, I replaced it with esbuild's --alias option.

sh
npx esbuild src/app.tsx --bundle --format=iife --jsx=automatic \
  --define:process.env.NODE_ENV='"production"' --minify \
  --alias:react-dom/client=react-dom/profiling --outfile=dist/profiling.js

react-dom/profiling loads the profiling build when NODE_ENV is production. With the build converted this way, onRender was called, as the results above show.

I have not tested other bundlers such as Vite or webpack. They should be able to make the same replacement with their own alias settings, but I have not confirmed this.

Point

Times measured in the development build were larger than in the profiling build. In the test environment, actualDuration for updates of the list without memo was 41.9 to 51.8 ms in the development build and 8.2 to 12.6 ms in the profiling build (in both cases, the three updates of the first run). Do not read the development build numbers as the real speed in production.

Here are the results from the development build.

text
--- build=dev case=basic call count=4
App      mount         actual=  58.30 base=  44.90 start=  183.10 commit=  245.90
App      update        actual=  51.80 base=  50.70 start=  384.30 commit=  440.00
App      update        actual=  42.70 base=  42.70 start=  578.10 commit=  624.00
App      update        actual=  41.90 base=  42.00 start=  761.20 commit=  805.10

When I ran the same test again, the values changed. For example, in the profiling build, baseDuration for the memo case was 13.7 to 13.9 ms on the first run and 10.6 to 10.8 ms on the second. Do not decide from a single run. It is safer to measure several times and compare.

Choosing between React Developer Tools and Profiler, and the overhead

<Profiler> is a way to receive timing data in code. If you want to watch while you interact with the page, the Profiler tab in React Developer Tools is useful. The official reference on measuring performance says a similar capability is available as a browser extension. I have not tested the React Developer Tools browser extension.

The official reference also notes that <Profiler> is lightweight, but each one adds a small amount of CPU and memory cost, so it should be used only when needed.

​Posted by 86