الانتقال إلى المحتوى
FrontendReact· 7 دقائق للقراءة

Memoizing Everything Won't Speed Up React: useMemo, useCallback and the Compiler

A cached value only helps when something compares it, and one new object breaks the whole chain.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

Typing a note on the sales dashboard lags, so a pull request titled "perf: memoize everything" lands. It wraps every derived value in useMemo and every handler in useCallback, thirty changes in all. The React Profiler tells the same story before and after: each keystroke still re-renders the chart with thousands of points. Nothing in the chart's path checks whether its props are the same objects as last time, and one of those props is brand new on every render anyway.

The common misconception

The belief: memoization is free insurance. A cache can only skip work, so wrapping every value and every function means React does less, and the worst case is that it does nothing.

Smart developers believe this because the Hooks sound like optimizations and are often taught as one. It's also half true. React's docs say that memoizing everything does no significant harm, and some teams choose to do exactly that so they don't have to think about each case. But "no harm" isn't "faster". The docs list only a few cases where these Hooks help and say there's no benefit in the rest. Meanwhile the code gets harder to read, and the team believes a slow screen is already optimized.

What actually happens

A cache only helps someone who reads it

still runs on every render. React compares each dependency with its previous value using Object.is, then returns the cached value or recomputes it. useCallback(fn, deps) is effectively useMemo(() => fn, deps): your arrow function is still created on every render, and React hands back the old one when the dependencies match.

What you get is : the same object or function as last time. That pays off only when something checks it. The docs name three cases: a calculation that's noticeably slow while its dependencies rarely change, a prop passed to a component wrapped in memo, and a value used as a dependency of another Hook, like useEffect. A stable handler on a <button> or on a child without memo changes nothing, because that child renders whenever its parent does. Caching a short string is pointless too, since strings already compare by value. And useMemo never speeds up the first render; it only skips work on updates.

Memoization is a chain

memo makes a component skip a render when every prop is Object.is-equal to the previous one. So the child must be wrapped in memo, and every prop must be stable. An inline object, an inline arrow or an array built in JSX is new on every render, and as the docs put it, a single always-new value is enough to break memoization for an entire component. All the other cached props feeding it are wasted.

TSX SalesPanel.tsx
import { useCallback, useMemo, useState } from "react";export function SalesPanel({ sales, region }: Props) {  const [note, setNote] = useState("");  const title = useMemo(() => `Sales: ${region}`, [region]);  const points = useMemo(() => toPoints(sales), [sales]);  const onSelect = useCallback((id: string) => openSale(region, id), [region]);  // ChartView isn't wrapped in memo: stable props change nothing  return (    <>      <textarea value={note} onChange={(e) => setNote(e.target.value)} />      <ChartView        title={title}        points={points}        options={{ animate: false }} // a new object on every render        onSelect={onSelect}      />    </>  );}

In the right version every link holds. Chart is wrapped in memo, options is a module constant, points and onSelect are cached because a memo component receives them, and title is a plain string. Typing now re-renders SalesPanel and the textarea, and the chart skips its render. It also has one less Hook than the version that "memoized everything" and didn't work.

What React Compiler changes

reached its first stable release, 1.0, on October 7, 2025. It's a build-time tool, mainly the babel-plugin-react-compiler Babel plugin, that analyzes components and Hooks and memoizes them for you. The docs say it applies the equivalent of memo to every component, and that its memoization is in most cases as precise as handwritten code, or more. It works best with React 19 and also supports React 17 and 18.

It isn't magic. The compiler relies on your code following the Rules of React, and it skips any component or Hook it can't safely optimize, which the compiler-powered rules in eslint-plugin-react-hooks report. For new code, the docs recommend letting the compiler memoize and keeping useMemo and useCallback as escape hatches for precise control, such as a value used as an Effect dependency. For existing code, leave the memoization in place or test carefully before removing it, because removing it can change what the compiler outputs.

MythReality
Memoizing a value makes React do lessOnly when a memo component, a dependency array or a slow calculation uses the cache
useCallback avoids creating the functionThe function is created on every render; React returns the old one
Caching most props is enoughOne always-new prop breaks memo for the whole component
useMemo speeds up the first renderIt only skips work on updates
The compiler means deleting every useMemoKeep existing memoization or test before removing it

How to apply it in your project

  1. Profile the slow interaction firstRecord it in the React DevTools Profiler, ideally on a production build with CPU throttling. Look for an expensive component that renders with the same props; that's the only place memo can help.
  2. Complete the chain where it mattersWrap that component in memo, then make every prop stable: hoist constants out of the component, pass primitives instead of objects, and cache the rest with useMemo and useCallback.
  3. Remove memoization nobody readsWithout the compiler, delete useCallback on handlers for DOM elements or children without memo, and useMemo around cheap strings and numbers. The code gets shorter and nothing gets slower.
  4. Turn on React CompilerInstall babel-plugin-react-compiler first in your Babel pipeline, or use your framework's option, and enable the recommended preset of eslint-plugin-react-hooks to see what it skips. Compiled components show a "Memo" badge in React DevTools.

Key takeaways

  • useMemo and useCallback only pay off when a memo component, a dependency array or a slow calculation uses the cached value.
  • Memoization is a chain: the child needs memo and every prop must be stable, and one new object breaks it.
  • Keeping state local and passing JSX as children often removes the need for memoization entirely.
  • React Compiler 1.0 memoizes components and Hooks at build time; write new code plainly and keep the Hooks as escape hatches.

📚 المصادر

  1. useMemo — React API Reference
  2. useCallback — React API Reference
  3. memo — React API Reference
  4. Introduction to React Compiler — React
  5. React Compiler v1.0 — React Blog

التعليقات (0)