# Measuring React performance

How to produce the one number the fix will be judged against. Open this when the user has no baseline, or when you need the exact recording steps for a tool.

Sources: https://react.dev/blog/2025/10/01/react-19-2 · https://github.com/GoogleChrome/web-vitals · https://developer.chrome.com/docs/devtools/performance

## 1. Build the thing you are measuring

Dev servers are not the app. React StrictMode double-invokes render, nothing is minified, and the compiler output differs.

```bash
# Next.js
next build && next start
# Vite / React Router
vite build && vite preview
```

Throttle to the users you care about: Chrome DevTools → Performance → CPU **4× slowdown** and network **Slow 4G**, or better, a real mid-range Android. Record the device, build, network and interaction with every number — a baseline without conditions cannot be reproduced.

## 2. React Performance Tracks (React 19.2+)

React 19.2 emits its own tracks into the Chrome Performance panel: a **Scheduler** track (what React is working on, and at which priority: blocking, transition, idle) and a **Components** track (which component tree rendered, mounted or ran effects, and for how long).

1. Open the app in a Chrome profile with no extensions (extensions distort INP).
2. DevTools → Performance → Record → perform the single interaction → Stop.
3. Read top-down: input event → Scheduler track entry → Components track subtree → Commit → Paint.
4. A long **Components** entry means render cost. A long gap before it means the update was queued behind other work — a scheduling problem, not a render problem. A long paint after a short commit is a CSS/layout problem.

Use these tracks before the Profiler when the complaint is "the interaction feels slow"; use the Profiler when the complaint is "it re-renders too much".

## 3. React DevTools Profiler

1. Profiler tab → gear → enable **Record why each component rendered**.
2. Record → one interaction → stop. Read the flamegraph of the **slowest commit**, not the first.
3. For each expensive component the panel names the cause: props changed (which ones), state changed, context changed, parent rendered.
4. Ranked chart answers "what costs the most"; the commit list answers "how many commits did one keystroke cause".

Record the two numbers worth quoting: commits per interaction, and duration of the slowest commit.

## 4. Field data: INP attribution

Lab numbers mislead on INP. Ship the attribution build and log the slow interactions from real users:

```ts
import { onINP, onLCP, onCLS } from 'web-vitals/attribution';

onINP((metric) => {
  const a = metric.attribution;
  sendToAnalytics({
    name: metric.name,                // 'INP'
    value: metric.value,              // ms
    rating: metric.rating,            // 'good' | 'needs-improvement' | 'poor'
    target: a.interactionTarget,      // CSS selector of the element
    inputDelay: a.inputDelay,         // queued behind other work
    processingDuration: a.processingDuration, // your event handlers
    presentationDelay: a.presentationDelay,   // render + paint after the handler
    longestScript: a.longestScript,   // script attribution (web-vitals 5)
  });
});
onLCP((m) => sendToAnalytics({ name: m.name, value: m.value, element: m.attribution.element }));
onCLS((m) => sendToAnalytics({ name: m.name, value: m.value, source: m.attribution.largestShiftTarget }));
```

Read the split before choosing a fix:

| Dominant part | Meaning | Fix direction |
|---|---|---|
| `inputDelay` | The main thread was busy when the user tapped | Cut long tasks, defer third-party scripts, `scheduler.yield()` |
| `processingDuration` | Your handler is slow | Do less in the handler; move heavy work into a transition or a worker |
| `presentationDelay` | The render/paint after the update is slow | Fewer components re-rendering, virtualise, simplify CSS |

`onLCP`'s `attribution.element` names the LCP element — always identify it before touching images.

## 5. Memory

1. DevTools → Memory → **Heap snapshot** on the suspect screen.
2. Navigate away and back five times, force GC (trash icon), snapshot again.
3. Compare snapshots ("Objects allocated between snapshot 1 and 2"), sort by **Retained size**, filter for `Detached` to find DOM kept alive by a listener or closure.

Growth that never returns after GC across five cycles is a leak; a sawtooth that returns is normal.

## 6. Bundles

```bash
# Next.js — add @next/bundle-analyzer to next.config, then:
ANALYZE=true next build
# Vite
npx vite-bundle-visualizer
# any build with source maps
npx source-map-explorer 'dist/assets/*.js'
```

Look for: one chunk far larger than the rest, the same library in two chunks, a whole icon or locale pack, two copies of React in a monorepo, and server-only modules pulled into a `'use client'` graph.

## 7. Guard the number in CI

- `size-limit` with a per-entry byte budget, run on every pull request; it fails the build on regression and prints the delta.
- Lighthouse CI (`lhci autorun`) with asserted budgets for LCP, CLS and total JS on the two or three routes that matter.
- Keep budgets in the repo next to the code, and update them deliberately in the pull request that spends the budget.

## 8. What to write down

```
Metric: INP 420 ms (p75, field, last 7 days)
Where: /orders, "Apply filter" button
Conditions: production build, Moto G Power, Slow 4G, Chrome 141
Split: inputDelay 60 / processing 300 / presentation 60
```

That block is the baseline. Every fix is re-measured against it in the same conditions.
