Measure a costly render calculation
Measure before optimizing
Memoization saves repeated work by reusing a previous result when the inputs are equivalent. It has costs: comparisons, retained memory and extra code. Begin with a correct plain implementation, then measure a specific slow interaction. This chapter uses React 18/19 public APIs in ordinary, compiler-disabled JSX; the exact patched dependencies and React measurements remain pending. Each example is an independent App.jsx. The fictional archive catalogue contains synthetic records, not external data.
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';function summarize(rows, query) { const term = query.trim().toLowerCase(); const matches = rows.filter(row => row.title.toLowerCase().includes(term)); return {count: matches.length, pages: matches.reduce((sum,row)=>sum+row.pages,0)};}function makeRows(size) { return Array.from({length:size}, (_,i)=>({id:i,title:i%2===0?'Atlas '+i:'Journal '+i,pages:i%5+1}));}const rows=makeRows(20000);export default function App(){const [query,setQuery]=useState('atlas');const [compact,setCompact]=useState(false);const result=summarize(rows,query);return <section style={{padding:compact?4:20}}><label>Search <input value={query} onChange={e=>setQuery(e.target.value)}/></label><button onClick={()=>setCompact(v=>!v)}>Toggle spacing</button><p>{result.count} records, {result.pages} pages</p></section>;}Initially 10,000 records match Atlas and their pages total 30,000. Toggle spacing changes padding without changing the query or the summary. Nevertheless the plain component recalculates the summary when it renders. A small calculation may be cheap enough to leave alone. Use the same input dataset, interaction and build mode for comparison. An expensive first render is not fixed by caching its result for subsequent renders.
Separate identity from equal contents
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';export default function App(){const a={category:'atlas'};const b={category:'atlas'};const alias=a;return <ul><li>Two objects: {String(Object.is(a,b))}</li><li>Same object: {String(Object.is(a,alias))}</li><li>Same text: {String(Object.is(a.category,b.category))}</li></ul>;}The results are false, true and true. Object identity matters even when fields match. Object.is also treats NaN as equal to itself and distinguishes positive and negative zero. Immutable updates allow identity to signal change; mutating an existing array can hide a change from dependency comparisons. Prefer passing simple values where possible.
Set up a fair comparison
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';function summarize(rows, query) { const term = query.trim().toLowerCase(); const matches = rows.filter(row => row.title.toLowerCase().includes(term)); return {count: matches.length, pages: matches.reduce((sum,row)=>sum+row.pages,0)};}function makeRows(size) { return Array.from({length:size}, (_,i)=>({id:i,title:i%2===0?'Atlas '+i:'Journal '+i,pages:i%5+1}));}const rows=makeRows(20000);function Plain({query}){const data=summarize(rows,query);return <p>{data.count} records, {data.pages} pages</p>;}function Cached({query}){const data=useMemo(()=>summarize(rows,query),[query]);return <p>{data.count} records, {data.pages} pages</p>;}function report(id,phase,actualDuration,baseDuration,startTime,commitTime){console.table({id,phase,actualDuration,baseDuration,commitTime});}export default function App(){const [query,setQuery]=useState('atlas');const [tick,setTick]=useState(0);return <section><label>Search <input value={query} onChange={e=>setQuery(e.target.value)}/></label><button onClick={()=>setTick(n=>n+1)}>Unrelated update</button><p>Updates: {tick}</p><Profiler id="plain" onRender={report}><Plain query={query}/></Profiler><Profiler id="cached" onRender={report}><Cached query={query}/></Profiler></section>;}Both summaries should stay equal when you click Unrelated update; changing the search should update both. Profiler reports rendering work for a committed update, not complete user-perceived latency. actualDuration measures work in the profiled subtree for that update; baseDuration estimates the cost without optimizations. Use the supplied benchmark for pure calculation timings and the separate checklist for real React measurements. Alternate trial order and warm up, compare medians, retain raw observations, and record browser, device, build mode and compiler status. Development StrictMode can run render calculations extra times. A normal production build disables programmatic profiling; use a supported profiling build when collecting these callbacks. Never invent render counts or speedup claims.
Practice
Run the standalone benchmark; compare equal outputs before timing. Then in an approved React environment record mount, unchanged-query updates and changed-query updates for both Profiler subtrees. Explain why the two timing sets measure different things.