Introduction to the React Compiler
Relevant source files
The following files were used as context for generating this wiki page:React Compiler is an optimizing compiler for React applications. Its primary purpose is to automatically memoize components and hooks, ensuring that only the minimal necessary parts of the UI re-render when state changes. This eliminates the need for manual optimizations like useMemo, useCallback, and React.memo. In addition to performance enhancements, the compiler also validates that components and hooks adhere to the Rules of React.
The compiler is implemented as a Babel plugin, babel-plugin-react-compiler, and works in conjunction with a runtime package, react/compiler-runtime. It analyzes the component code and transforms it into an optimized version that leverages a caching mechanism to skip redundant work.
Sources: compiler/README.md, compiler/packages/babel-plugin-react-compiler/package.json, compiler/packages/react-mcp-server/src/index.ts:90-93
Core Concepts
Automatic Memoization
The key feature of the React Compiler is its ability to automatically apply memoization. By analyzing dependencies within components, it can wrap expressions in a cache, preventing re-computation and re-rendering if the inputs have not changed. This simplifies the developer experience by removing the burden of manual memoization.
When the compiler successfully optimizes a component, it injects an import for the compiler runtime and initializes a cache.
// Successful optimization adds this import
import { c as _c } from "react/compiler-runtime";
// A cache is initialized, where n is the size of the cache
const $ = _c(n);
Sources: compiler/packages/react-mcp-server/src/index.ts:446-449, packages/react-devtools-shared/src/tests/compiler-integration-test.js:63-65
The Babel Plugin
The compiler is distributed as a Babel plugin named babel-plugin-react-compiler. This plugin integrates into the build process of a React application, transforming JSX code before it is bundled.
Sources: compiler/packages/babel-plugin-react-compiler/package.json:2-3
Compiler Runtime
The optimizations applied by the compiler rely on a small runtime library. This runtime is available as part of the main react package and can be imported from react/compiler-runtime. The primary function exported is c (aliased as _c or useMemoCache), which is used to create the memoization cache within compiled components.
Sources: packages/react/package.json:28-31, packages/react-devtools-shared/src/tests/compiler-integration-test.js:19-20
Architecture and Compilation Flow
The React Compiler processes code through a series of internal passes to analyze, transform, and optimize components. This process can be visualized as a pipeline that takes standard React code as input and produces memoized, high-performance JavaScript code as output.
This diagram illustrates the high-level compilation flow.
The compiler can "bail out" of optimizing a component if it encounters code that violates the Rules of React or contains patterns it cannot safely optimize. When this happens, it may produce diagnostic messages to help the developer fix the underlying issue.
Sources: compiler/packages/react-mcp-server/src/index.ts:94-98
Tooling and Analysis
The React ecosystem includes powerful tools for interacting with and analyzing the output of the compiler. A key component is the react-mcp-server, which provides a suite of utilities for compilation, performance review, and debugging.
MCP Server (react-mcp-server)
The react-mcp-server is a server implementing the Model-Context Protocol (MCP) that exposes several tools for developers. These tools allow for direct interaction with the compiler and runtime performance analysis.
Sources: compiler/packages/react-mcp-server/src/index.ts:24-28
The server provides the following key tools:
| Tool Name | Description |
|---|---|
compile | Compiles React code and returns the optimized output or diagnostic messages. |
review-react-runtime | Measures the performance of React components to verify improvements. |
query-react-dev-docs | Searches the official react.dev documentation. |
parse-react-component-tree | Parses the component tree from a running React application. |
Sources: compiler/packages/react-mcp-server/src/index.ts:30, compiler/packages/react-mcp-server/src/index.ts:84, compiler/packages/react-mcp-server/src/index.ts:271, compiler/packages/react-mcp-server/src/index.ts:329
The compile Tool
This tool is the primary interface for running the React Compiler on a piece of code. It can be used to check if code will compile successfully and to inspect the output of various compiler passes.
The following diagram shows a typical interaction with the compile tool.
Parameters:
| Parameter | Type | Description |
|---|---|---|
text | string | The source code to compile. |
passName | 'HIR', 'ReactiveFunction', 'All', '@DEBUG' (optional) | Specifies an internal compiler pass to inspect. If omitted, the final compiled code is returned. |
Sources: compiler/packages/react-mcp-server/src/index.ts:101-105
The review-react-runtime Tool
This tool is designed to verify that compiler optimizations or other code changes actually improve performance. It runs the provided code and measures key web vitals and React Profiler metrics.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
text | string | The React component code to measure. Must contain a non-arrow App functional component. | |
iterations | number | 2 | The number of times to run the performance measurement. |
Sources: compiler/packages/react-mcp-server/src/index.ts:299-303
DevTools Integration
The React Compiler integrates with React DevTools to provide visual feedback on which components have been successfully optimized. When a component is compiled, its display name in the DevTools component tree is prefixed with Forget. This prefix is added when the component uses the useMemoCache hook from the compiler runtime.
// Test case from DevTools integration tests
it('If useMemoCache used, the corresponding displayName for a component should have Forget prefix', () => {
const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
const reactDOMFiberRendererInterface = hook.rendererInterfaces.get(1);
const Foo = () => {
// useMemoCache is imported from 'react/compiler-runtime'
const $ = useMemoCache(1);
// ...
};
act(() => render(<Foo />));
// The display name for Foo will be "Forget(Foo)"
expect(
reactDOMFiberRendererInterface
.getDisplayNameForElementID(2)
.indexOf('Forget'),
).toBe(0);
});
Sources: packages/react-devtools-shared/src/tests/compiler-integration-test.js:59-83
Summary
The React Compiler is a significant evolution in the React ecosystem, aiming to deliver high-performance applications by default. By automating memoization, it simplifies codebases and allows developers to focus on application logic rather than manual performance tuning. Its integration with Babel and rich analysis tools like the MCP server and DevTools provides a comprehensive system for building, optimizing, and verifying modern React applications.
Sources: compiler/README.md, compiler/packages/react-mcp-server/src/index.ts:431-436