Profile a measured interaction
Measure an interaction before optimizing
Performance profiling means recording where time is spent during a particular workload. Start with a question: why does changing the search field in a catalogue feel slow? A React render calculates the next UI, a commit applies changes, and the browser performs layout and painting as needed. Network waits and input delays also affect how the interaction feels. A slow interaction may involve only one render; a fast interaction may involve several. Use a fixed synthetic dataset, a repeatable interaction and the same build and device for comparisons. This chapter uses React 19 as a proposed teaching baseline; exact dependency compatibility is not established.
export function createCommitLog(){ const samples=[]; return { onRender(id,phase,actualDuration,baseDuration,startTime,commitTime){ samples.push({id,phase,actualDuration,baseDuration,startTime,commitTime}); }, read(){return samples.map(s=>({...s}));}, clear(){samples.length=0;} };}Create one collector outside component rendering for each trial. The callback receives information about a committed update of the wrapped tree: actualDuration estimates the render work for that update, baseDuration estimates rendering the subtree without memoization, and commitTime identifies the commit timestamp (not the length of the commit). Mount, update and nested-update phases can occur. Nested profilers may overlap, so adding their durations can double count work. This collector is deliberately limited to short local experiments; clear it between trials and do not store user data.
Record one predictable update
import React,{Profiler,useState} from 'react';import {createCommitLog} from './c42-l01-s03.mjs';export const log=createCommitLog();const seeds=Array.from({length:600},(_,i)=>({id:i,name:`${i%2?'Bean':'Pea'} ${i}`}));function Results({query}){ const matching=seeds.filter(s=>s.name.toLowerCase().includes(query.toLowerCase())); return <ul>{matching.map(s=><li key={s.id}>{s.name}</li>)}</ul>;}export default function App(){ const [query,setQuery]=useState('pea'); const [count,setCount]=useState(0); return <main><label>Filter <input value={query} onChange={e=>setQuery(e.target.value)}/></label> <button type="button" onClick={()=>setCount(c=>c+1)}>Unrelated count: {count}</button> <Profiler id="results" onRender={log.onRender}><Results query={query}/></Profiler></main>;}Expected behavior: 300 pea entries initially; the counter changes without changing those entries; bean matches 300 different entries. These are acceptance criteria, not observed React measurements. Use React DevTools Profiler to record just the interaction and inspect the commit flame chart for expensive subtrees. Use the browser Performance panel for the event, scripting, layout, paint and other main-thread work. Inspect the Network waterfall for waits, request dependencies and cache behavior. Keep the device, browser, throttling, compiler settings and data fixed. Development builds and StrictMode can change timing and execution; use a supported production profiling build for representative React measurements, noting its overhead. Normal production builds disable Profiler instrumentation by default.
export function summarize(values){ if(!values.length || values.some(v=>!Number.isFinite(v)||v<0)) throw new RangeError('Expected nonnegative finite durations'); const sorted=[...values].sort((a,b)=>a-b); const mid=Math.floor(sorted.length/2); return {count:sorted.length,min:sorted[0],median:sorted.length%2?sorted[mid]:(sorted[mid-1]+sorted[mid])/2,max:sorted.at(-1)};}For synthetic input [8, 2, 4, 6], the result is count 4, minimum 2, median 5 and maximum 8. This is a calculation example, not measured React performance. Preserve raw samples and distinguish mount from repeated updates; warm up first. If you see a heavy component, inspect its props and state ownership, then change one factor and repeat. Do not treat fewer commits as a success unless the user-facing result and latency improve.