React Performance Patterns Developers Often Get Wrong

Find it before your users do.

Himanshu Srivastava · WeAreDevelopers India

Fast at launch.
Still fast?
Live

Launch day.

Month nine.

–renders per keystroke
–ms per keystroke
20 seconds · shout it out

Find the bug.

function App() {
  const [user] = useState(USER);
  const [search, setSearch] = useState("");
  return (
    <AppContext.Provider value={{ user, search, setSearch }}>
      <Dashboard />
    </AppContext.Provider>
  );
}

function Cell({ order }) {
  const { user } = useContext(AppContext);
  ...

How I hunt every one of these

01

Symptom

What the user feels

02

Find

The tool that sees it

03

Explain

Where AI helps, and where it lies

04

Fix

Smallest change

05

Measure again

Or it didn't happen

One idea first: a render is a function call

Render 1

count = 0
options = {…} #a1
onClick = fn #f1

Render 2

count = 1
options = {…} #b7
onClick = fn #f2

Render 3

count = 1
options = {…} #c4
onClick = fn #f3

Every render is a snapshot. New objects, every time.

Trap 1 · Render cascades · Find it

React DevTools › Profiler

Profiler settings: Record why each component rendered while profiling Flamegraph: 900 Cells re-rendered, Why did this render? Context changed

Those thin bars are 900 Cells · hover one: “Context changed”

Trap 1 · Render cascades · Find it

Ask the profiler why

$ npx @dev-with-coffee/whyslow dashboard.json 4 commits · 7,228 renders · slowest 48.8 ms CONTEXT Cell: 900 instances re-render per commit because a context changed triggered by App. Everything under them re-renders too: Tile (900) SLOW 4 of 4 commits missed a frame (2.9x a 16.7 ms frame)

Profiler › Save profile › whyslow reads the whole thing for you

Trap 1 · Fix it

search state moved downOne keystroke. 901 renders.

–rendered per keystroke
–ms per keystroke
Trap 1 · The code

Move the state to where it's used

function App() {
  const [user] = useState(USER);
  const [search, setSearch] = useState("");
  return (
    <AppContext.Provider value={{ user, search, setSearch }}>
      <Dashboard />   // 900 Cells read this context
    </AppContext.Provider>
  );
}
function App() {
  return (
    <UserContext.Provider value={USER}>
      <Dashboard />   // Cells only read the user
    </UserContext.Provider>
  );
}

function SearchPanel() {
  const [search, setSearch] = useState("");   // lives here now
  ...
Trap 1 · Measure again

Same four letters. Before and after.

$ npx @dev-with-coffee/whyslow before.json after.json slowest commit 48.8 ms → 0.5 ms -99% renders per commit 1807 → 3 -100% Gone after the change ✓ Cell: 900 instances re-render because a context changed
Trap 2 · Memoization

Half a chain does nothing

useCallback, no memo

48 renders

memo + inline style

48 renders

memo + a stable style

0 renders

whyslow: List is memo'd, but its `style` prop changed in 8 of 8 commits

Trap 2 · Memoization · Find it

It names the prop

List (Memo) re-rendered: Props changed: (style)

List is memo'd, and it still rendered: Props changed: (style)

Trap 2 · The code

One line apart

// 1 · useCallback, no memo          → 48 renders
const onSelect = useCallback(id => setSelected(id), []);
<List onSelect={onSelect} style={{ padding: 8 }} />

// 2 · memo + inline style           → 48 renders
const MemoList = memo(List);
<MemoList onSelect={onSelect} style={{ padding: 8 }} />

// 3 · memo + a stable style         → 0 renders
const STYLE = { padding: 8 };   // outside the component
<MemoList onSelect={onSelect} style={STYLE} />
Memo only pays when something compares.
Trap 3 · Dependency arrays

Nobody touched anything

function Results({ query }) {
  const options = { query, limit: 20 };

  useEffect(() => {
    fetchResults(options);
  }, [options]);
}
function Results({ query }) {
  useEffect(() => {
    const options = { query, limit: 20 };
    fetchResults(options);
  }, [query]);
}
live
0requests sent
Trap 3 · Dependency arrays · Find it

11 commits in 2 seconds. Nobody typed.

11 commits in 2 seconds, Results re-rendered because the parent rendered

Profiler: a commit every 250 ms · Network tab: a request with every one

The next two
The profiler can't see them.
Trap 4 · Stale closures

Stuck at 1. Forever.

const [count, setCount] = useState(0);

useEffect(() => {
  const id = setInterval(() => {
    setCount(count + 1);
  }, 400);
  return () => clearInterval(id);
}, []);
const [count, setCount] = useState(0);

useEffect(() => {
  const id = setInterval(() => {
    setCount(c => c + 1);
  }, 400);
  return () => clearInterval(id);
}, []);
live

Found by: react-hooks lint, and AI reading the code

Trap 5 · The leak

It only hurts after hours

useEffect(() => {
  const onMsg = () => setUnread(n => n + 1);
  bus.on(onMsg);
  // no cleanup
}, []);
useEffect(() => {
  const onMsg = () => setUnread(n => n + 1);
  bus.on(onMsg);
  return () => bus.off(onMsg);
}, []);
live · 0 route changes
0listeners attached
0handlers per message
Your turn
“My dashboard search lags. Why?”

Shout it: what does the AI say first?

AI is only as good as your evidence

Just the symptom

  1. Virtualize the list
  2. Memo the tiles
  3. Debounce the input
  4. useDeferredValue
  5. useMemo the filter

Five guesses. None is the cause.

The same question + the profile

  • App holds search in a context
  • Cell re-renders ×900 per letter, reason: context
  • Move the state down, or split the context
  • “Send me Cell's code to be sure”

The cause. Then it asked for proof.

Open source · today

npx whyslow profile.json

$ whyslow profile.json # what re-rendered, and why $ whyslow before.json after.json # did the fix work? $ whyslow profile.json --ai # evidence pack for any AI
QR code for the whyslow repo

github.com/himanshuSri24/whyslow

The obvious question
“Doesn't React Compiler just fix all this?”
Measured · React 19.3 + Compiler 1.0

Three fixed. Two untouched.

TrapMeasurePlainCompiled
Render cascadems per keystrokeFixed
Broken memo chainrow renders in 1 sFixed
Dependency arraysrequests in 2 sFixed
Stale closurecounter after 10 ticksStill broken
Listener leakhandlers per messageStill broken
From the LinkedIn comments
[Best bug from the comments]

Credit: [name]

Symptom → tool

Typing or scrolling lagsProfiler + “why it rendered”
Requests nobody asked forNetwork tab + Profiler timer
A value that's old or wrongreact-hooks lint, AI on the code
Slower the longer it's opengetEventListeners, heap snapshots
Did my fix work?Same recording, before and after
Tools find it.
AI explains it.
You decide.

Thank you.

Himanshu Srivastava
devwithcoffee.com

Come argue
with me
on LinkedIn

LinkedIn QR code
Backup · Only if someone asks

Slow response A arrives after fast response B

useEffect(() => {
  const ctrl = new AbortController();
  fetch(url, { signal: ctrl.signal })
    .then(r => r.json())
    .then(setRows)
    .catch(ignoreAbort);
  return () => ctrl.abort();
}, [filter]);

Switch filter A → B. If A is slower, its response lands last and B's screen shows A's data.

Cleanup cancels the old request. The compiler can't help here either: it's logic.

Backup · Only if someone asks

It skips silently

const [, setS] =
useState(0);
useCallback(…, [])

"Existing memoization could not be preserved"

ref.current += 1;

// during render

"Cannot access refs during render"

props.o.x = 1;

// mutating a prop

"This value cannot be modified"