The React Compiler Wrote You a Seven-Slot Cache

The compiler's output is a slot array and a chain of guarded assignments. Reading one compiled component explains what it caches, what it skips, and when it declines.

A cast steel rack of seven identical slots with a spring-loaded indexing bar across the top, a single brass tag standing upright in the first slot.

One component, compiled with [email protected]:

jsx
export function Greeting({ name, items }) {
  const upper = name.toUpperCase();
  const visible = items.filter(i => i.active);
  return <div title={upper}>{visible.length}</div>;
}
js
import { c as _c } from "react/compiler-runtime";
export function Greeting(t0) {
  const $ = _c(7);
  const { name, items } = t0;
  let t1;
  if ($[0] !== name) { t1 = name.toUpperCase(); $[0] = name; $[1] = t1; }
  else { t1 = $[1]; }
  const upper = t1;
  // …two more blocks in the same shape
}

An array of seven slots and a run of if statements. The entire discussion about whether to enable this thing is a discussion about that array, and almost nobody reads it.

_c is a hook, not a helper

The name looks like a utility. It is not. react/compiler-runtime resolves to a file whose whole export is this:

js
exports.c = function (size) {
  var dispatcher = ReactSharedInternals.H;
  // …a console.error if there is no dispatcher…
  return dispatcher.useMemoCache(size);
};

useMemoCache on the current dispatcher, which is how every other hook gets its storage. That single line explains three things at once: why the cache survives across renders of the same component instance, why it resets when the instance unmounts, and why the compiler is strict about the Rules of Hooks. A compiled component has one more hook call than you wrote, and it is the first one.

Reading the emitted file rather than the release notes is the same move as reading what await compiles to: the transformation is a text you can open, and opening it settles arguments that otherwise run on vocabulary.

The slot arithmetic is visible

The size passed to _c is not a heuristic. Compile three components that differ only in how much they derive:

jsx
export function Zero()        { return <i>x</i>; }
export function One({ a })    { return <i>{a.toUpperCase()}</i>; }
export function Two({ a, b }) { return <i>{a + b}</i>; }
Zero -> _c(1)     One -> _c(4)     Two -> _c(2)

One is the straightforward case: two cached values, each costing one slot for its dependency and one for its result. Zero has nothing to compare against, so the compiler uses a marker instead of a comparison:

js
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = <i>x</i>; $[0] = t0; }

One slot, filled on the first render and read on every later one. That is a constant element hoisted into per-instance storage rather than module scope, which is what lets it still be a fresh element per instance.

What it refuses to cache is the interesting part

Two takes two props and uses two slots, not four. Here is why:

js
const t1 = a + b;                       // not cached
let t2;
if ($[0] !== t1) { t2 = <i>{t1}</i>; $[0] = t1; $[1] = t2; }

a + b is recomputed on every render. The compiler declined to memoize it, and the decision is the right one: storing a number and comparing it costs more than adding two numbers. What gets a slot is what allocates. The JSX element is an object, and a new object breaks referential equality for everything downstream, so it is cached and guarded on the value it depends on.

The same instinct shows up as hoisting. The inline arrow in Greeting does not get a slot either — it leaves the component entirely:

js
t2 = items.filter(_temp);
// …
function _temp(i) { return i.active; }

A closure over nothing does not need to be created per render, so it is not. Compare that to what a developer writing this by hand does, which is either useMemo on the array and nothing on the predicate, or useCallback on the predicate and a second dependency array to maintain.

The dependency arrays you no longer maintain

The shape that makes the case is a component with state and a handler, because that is where hand-written memoization is easiest to get wrong:

jsx
export function Counter({ step, label }) {
  const [n, setN] = useState(0);
  const onClick = () => setN(v => v + step);
  return <button onClick={onClick}>{label}: {n}</button>;
}
js
const $ = _c(6);
const [n, setN] = useState(0);
let t1;
if ($[0] !== step) { t1 = () => setN(v => v + step); $[0] = step; $[1] = t1; }
// …element guarded on label, n and onClick, using $[2] through $[5]

Six slots: two for the handler, four for the element. The useState call is untouched and stays exactly where it was written.

The interesting slot is $[0]. The handler closes over step and setN, and only step reached the guard. A useState setter is stable across renders, so there is nothing to compare. That is the dependency array a useCallback should had, computed from the closure rather than typed out and then quietly left stale when someone adds a variable to the body.

When it declines, it says nothing in the output

Two components the compiler will not touch:

jsx
export function Mutates({ config }) { config.seen = true; return <span>{config.seen}</span>; }
export function ConditionalHook({ on }) { if (on) { const [v] = React.useState(0); return <b>{v}</b>; } return null; }

The diagnostics are specific, if you ask for them by passing a logger to the plugin:

EVENT CompileError | bail.jsx
   reason: This value cannot be modified
   description: Modifying component props or hook arguments is not allowed. Consider using a local variable instead
   severity: Error
EVENT CompileError | bail.jsx
   reason: Hooks must always be called in a consistent order, and may not be called conditionally.
   severity: Error

The output is the part worth noticing. Both components come through with their JSX transformed and nothing else changed: no _c import, no cache, no comment. A component that opted out of the compiler and a component the compiler rejected are indistinguishable in the emitted file. If you enable this and measure no improvement, “it did not compile” is a hypothesis you have to test by reading diagnostics, not by reading code.

The same bookkeeping, moved

Put the compiled Greeting next to a signals runtime and the resemblance is not a metaphor. A signal graph stores a value, stores what it was derived from, and recomputes when a comparison fails. That is the if ($[0] !== name) block. The difference is when the graph is built: a signals library builds it while the program runs by recording reads, and the React compiler builds it while the program is compiled by reading the source.

Each choice buys something. Recording at runtime handles dynamic dependencies a compiler cannot see. Deciding at build time costs nothing at runtime and needs no subscription bookkeeping, which is why the compiled output has no graph in it at all, only the answers the graph would have produced.

What to read, and what I did not measure

Compile one real component from your codebase and look at two numbers: the argument to _c, and how many of your useMemo calls the compiler made redundant. Then run the plugin with a logger over the whole tree and count the CompileError events, because that count is the real answer to “is this on”.

What I did not measure: render-time performance, in either direction. Everything above is the shape of the emitted code on [email protected] and [email protected], and whether seven slots of comparison beat three recomputations depends entirely on what the component actually does.

FAQ

Frequently asked

What is the $ array in React Compiler output?

It is the memo cache for one component instance, created by const $ = _c(n). Odd and even slots are used together — a dependency is stored so the next render can compare against it, and the computed result is stored beside it.

How do I tell whether the React Compiler skipped a component?

Read the output or the diagnostics, because the code alone will not tell you. A skipped component is emitted exactly as written, with no _c import and no cache. Passing a logger to the Babel plugin surfaces a CompileError event with the reason.

Arrow keys to move, Enter to open.