Skip to content

Measure First

The Course
•
20 min study

Guessing is the slow way

The intermediate course showed memo, useMemo and transitions. Applied by guesswork, they add complexity in the wrong places and miss the real problem. Today starts with measuring: which components render, why, and how long they take.

Who's slow? React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { Profiler, useState } from "react"; import Chart from "./Chart.jsx"; import { logRender } from "./logRender.js"; export default function App() { const [note, setNote] = useState(""); return ( <main> <h1>Study Hub</h1> <Profiler id="Note" onRender={logRender}> <label> Today's note{" "} <input value={note} onChange={(e) => setNote(e.target.value)} /> </label> <p className="muted">{note.length} characters</p> </Profiler> <Profiler id="Chart" onRender={logRender}> <Chart /> </Profiler> </main> ); } // Pretends to draw a detailed chart: about 30 ms of work every render. export default function Chart() { const start = performance.now(); while (performance.now() - start < 30) { // Busy on purpose. } return ( <section className="panel"> <h2>This week</h2> <p className="muted">Mon 30 · Tue 45 · Wed 20 · Thu 60 · Fri 55 minutes</p> </section> ); } // Logs each render of a <Profiler> subtree: which one, why, and how long it took. export function logRender(id, phase, actualDuration) { console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Clear the console, then type two letters in Today's note.

What you'll see

Four lines: Note update: 0 ms and Chart update: 30 ms, twice. Each keystroke re-renders the chart, and the chart costs 30 ms, almost two frames at 60 frames a second, to show the same five numbers.

Profiler

JSX
<Profiler id="Chart" onRender={logRender}>
  <Chart />
</Profiler>

<Profiler> measures everything inside it. After each commit, React calls onRender with:

  • id, the name you gave it;
  • phase: "mount" the first time, "update" after that;
  • actualDuration: milliseconds spent rendering this subtree for this commit:
JavaScript
export function logRender(id, phase, actualDuration) {
  console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`);
}

Wrap the main areas of a screen in profilers, use the app, and the console says where the time goes. Profiler adds a little overhead, and is switched off in production builds unless you opt in, so treat it as a development tool.

React Developer Tools

On your own computer, the React Developer Tools extension has a Profiler tab. Press record, use the app, press stop. It shows every commit as a bar, and every component's render time as a coloured block. Its settings include Record why each component rendered, which answers the next question: was it a state change, a parent render, or a context?

What to look for

  • Components that render when their output can't have changed, like the chart here. The next lesson fixes this one.
  • One expensive component, rendering often. Make it cheaper, or render it less.
  • Many cheap components, such as a long list. Render fewer of them: lesson 3.
Exercise

Profile the whole app

Wrap the entire <main> in a third profiler, App. Type a letter. How does App's duration compare with Note's and Chart's together, and why?

Your working space React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { Profiler, useState } from "react"; import Chart from "./Chart.jsx"; import { logRender } from "./logRender.js"; export default function App() { const [note, setNote] = useState(""); return ( <main> <h1>Study Hub</h1> <Profiler id="Note" onRender={logRender}> <label> Today's note{" "} <input value={note} onChange={(e) => setNote(e.target.value)} /> </label> <p className="muted">{note.length} characters</p> </Profiler> <Profiler id="Chart" onRender={logRender}> <Chart /> </Profiler> </main> ); } // Pretends to draw a detailed chart: about 30 ms of work every render. export default function Chart() { const start = performance.now(); while (performance.now() - start < 30) { // Busy on purpose. } return ( <section className="panel"> <h2>This week</h2> <p className="muted">Mon 30 · Tue 45 · Wed 20 · Thu 60 · Fri 55 minutes</p> </section> ); } // Logs each render of a <Profiler> subtree: which one, why, and how long it took. export function logRender(id, phase, actualDuration) { console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

The outer measurement includes its descendants. Nested durations overlap; they are not independent pieces to add twice.

Hint 2: a first step

Profilers nest. Each one reports its own subtree.

Show one solution
JSX
<Profiler id="App" onRender={logRender}>
  <main>...</main>
</Profiler>

Each keystroke logs App update: 30 ms or so, about the sum of the two inner profilers, because App's subtree contains both. Nested profilers let you drill down: start with the whole screen, then narrow to the part that's slow.

Was this article helpful?

Your response is saved on this device.