Skip to main content

Automatic memoization and manual memoization

React Compiler changes the default optimization strategy for modern React code.

Before Compiler, teams often asked: Where should I add memo, useMemo, or useCallback?

With Compiler, the better first question is: Is this component written according to the Rules of React so the compiler can optimize it safely?

Automatic memoization mental modelโ€‹

function SearchResults({ query, items }) {
const visibleItems = filterItems(items, query);
return <Results items={visibleItems} />;
}

Compiler can analyze which reactive inputs affect derived work and preserve safe results across renders.

Automatic memoization follows reactive dependencies
Reactive inputs
query ยท items ยท state ยท context
Derived work
visibleItems ยท callbacks ยท JSX
Reuse previous work

This reduces the need to write manual useMemo for many purely performance-driven cases.

React.memo before and after Compilerโ€‹

Without Compiler:

const ProductCard = memo(function ProductCard({ product }) {
return <article>{product.name}</article>;
});

With Compiler, component-level reuse can often be generated automatically.

EXPLICITManual memo
Developer identifies a boundary and declares a reuse contract.
ANALYZEDCompiler memoization
Build-time analysis can preserve components, expressions, JSX, or functions where safe.

Compiler is not simply wrapping every component in memo.

useMemo still has semantic usesโ€‹

Manual memoization remains useful when identity itself is part of an external or architectural contract.

const options = useMemo(
() => ({ locale, currency }),
[locale, currency]
);

Examples:

  • an Effect depends on a deliberately stable object;
  • an imperative third-party API stores an object identity;
  • a migration relies on an existing identity contract;
  • code must preserve behaviour across compiler-disabled and compiler-enabled paths.

useCallback can still express external identityโ€‹

const handleChange = useCallback((value) => {
setValue(value);
}, []);

useEffect(() => {
externalWidget.subscribe(handleChange);
return () => externalWidget.unsubscribe(handleChange);
}, [handleChange]);

Here callback identity is not just a render-performance hint; an external system stores it.

Performance identity vs semantic identityโ€‹

Ask why the identity must be stable
Performance-only identity
Stable only to avoid repeated React rendering work. Compiler can often own this.
Semantic / external identity
Subscriptions, imperative APIs, cache keys, or contracts depend on reference identity. Manual control may remain appropriate.

Do not delete existing memoization blindlyโ€‹

Existing memo, useMemo, and useCallback may influence behavior, not just speed.

1Existing manual memoization
2Enable Compiler
3Preserve behavior first
4Test + profile
5Remove only with evidence

Removal can affect:

  • Effect dependencies;
  • third-party subscription identity;
  • memoized children;
  • intentional shared references;
  • compiler output and optimisation coverage.

Treat deletion as a refactor, not cleanup by default.

Memoization does not fix architectureโ€‹

Priority of performance fixes
Architecture
State scope ยท component boundaries ยท data ownership
Algorithms / DOM volume / virtualization
Compiler / manual memoization

A giant component with poor ownership does not become good architecture because its calculations are memoized.

Manual memoization can be harmfulโ€‹

Overusing manual memoization adds:

  • dependency arrays to maintain;
  • stale-closure risk when dependencies are wrong;
  • custom comparison bugs;
  • extra complexity in reviews;
  • memo bookkeeping that may cost more than the avoided work.

Use it for a reason you can explain.

memo custom comparisons require careโ€‹

const Row = memo(RowImpl, arePropsEqual);

A custom comparison must account for all props that affect rendering and behavior. An incorrect comparator can preserve stale values or callbacks.

Do not write expensive deep comparisons to avoid a cheap render.

Compiler and derived objects/functionsโ€‹

Inline objects and callbacks are not automatically performance bugs in a compiler-enabled codebase.

function Toolbar({ itemId }) {
const actions = {
onOpen() {
openItem(itemId);
},
};

return <ActionBar actions={actions} />;
}

Focus on correctness first. Measure before adding identity scaffolding manually.

Decision guideโ€‹

Should I add manual memoization?
Only trying to pre-empt hypothetical rerendersPrefer Compiler + measurement
Profiler shows repeated expensive workCheck architecture, then memoization
External API requires stable reference identityManual memoization may be semantic
Existing manual memo in Compiler migrationPreserve until tests/profile justify removal
Huge list/DOM or slow algorithmFix underlying cost first

Common mistakesโ€‹

  • wrapping every component in memo even with Compiler enabled;
  • replacing architecture work with memoization;
  • deleting all useMemo/useCallback during migration;
  • using deep custom comparators for cheap children;
  • ignoring semantic identity required by external systems;
  • treating a new object/function on every render as automatically wrong.

Production review checklistโ€‹

  1. Is the performance problem measured?
  2. Is state located near the UI that owns it?
  3. Is DOM/list volume bounded?
  4. Is the compiler active for this code path?
  5. Is manual identity needed for an external/semantic contract?
  6. Does existing memoization have behavior implications?
  7. Do profiling results improve after the change?

Interview questionsโ€‹

Junior: Does React Compiler make useMemo invalid?

Mid-level: When can manual useCallback still be appropriate in a Compiler-enabled application?

Senior: Explain the difference between performance identity and semantic identity, and how that distinction changes a Compiler migration plan.

Summaryโ€‹

Compiler changes the default, not every possible contract
Correct React code
Automatic memoization
Semantic identity / measured hotspot

Referencesโ€‹