Polski

Tutorial · React · Flyweight

React performance in practice – rendering, memo, React Compiler, profiling and list virtualization

Where unnecessary renders come from and how to remove them without guessing. The React 19 render model, moving state down, memo/useMemo/useCallback (and when not to use them), React Compiler 1.0, the Profiler, useDeferredValue, virtualizing 10,000 rows and code splitting – with live render counters.

By · · 18 min read

Most performance problems in React are not caused by React being slow, but by it rendering too much and too often: state sits too high in the tree, a list has 10,000 rows in the DOM, and every keystroke in the search box recomputes half of the app. In this tutorial you will first understand when a component renders, then learn to measure it, and finally remove the problems in order from the cheapest fix to the most expensive one. Three examples show "before" and "after" render counters live.

Prerequisites: Node.js 22.22+ (24 or 26 LTS recommended), React at the hooks level (see the React tutorial), the React DevTools browser extension. The state management tutorial also helps.

11 → 1
renders of a slow component – after moving state down (10 keystrokes)
10,000 → ~20
rows in the DOM – after virtualizing the list
1.0
React Compiler – stable since October 2025
≤ 200 ms
INP – threshold for "good" interaction responsiveness

The render model: trigger, render, commit

"Rendering" in React means calling a component function and comparing the result with the previous one. Only the commit phase changes the DOM – and only where the result differs. A render without DOM changes is not a bug, but it costs CPU time, and with heavy components it blocks the main thread and hurts INP (Interaction to Next Paint).

Diagram: The three phases of an update in React 19

A component re-renders when:

  1. its state changes (useState, useReducer, an external store),
  2. its parent renders – by default always, whether or not the props changed,
  3. a context it reads changes.

Point 2 surprises people most often. A props change does not trigger anything by itself – it is the parent's render that passes new props. memo lets you break that chain, but only when the props are actually equal.

Step 1. Measure first

Before changing anything, record a profile:

  • React DevTools → Profiler: record an interaction, look at the flamegraph and the ranking of the slowest components. In the settings enable "Record why each component rendered while profiling" – the Profiler will tell you whether the cause was state, the parent or a context.
  • "Highlight updates when components render" in the DevTools settings highlights rendering components on the page. Great for quickly spotting that typing a letter refreshes the whole screen.
  • The Chrome Performance panel: since React 19.2 it shows dedicated React tracks (Scheduler and Components) next to long tasks and painting.
  • <Profiler onRender> measures cost in code – in the project we log commits longer than 16 ms. actualDuration is the time of this render, baseDuration an estimate without any memoization.

Measure a production build (npm run build && npm run preview) with CPU throttling enabled in DevTools. Development mode and StrictMode (double rendering) inflate the numbers.

Project configuration with React Compiler

package.json
{
  "name": "pierogi-perf",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@tanstack/react-virtual": "^3.14.28",
    "react": "^19.3.1",
    "react-dom": "^19.3.1"
  },
  "devDependencies": {
    "@babel/core": "^8.1.3",
    "@rolldown/plugin-babel": "^0.3.2",
    "@types/react": "^19.3.3",
    "@types/react-dom": "^19.3.0",
    "@vitejs/plugin-react": "^6.3.0",
    "babel-plugin-react-compiler": "^1.0.0",
    "typescript": "~7.0.2",
    "vite": "^8.4.0"
  }
}
tsconfig.json
{
  "compilerOptions": {
    "target": "es2023",
    "lib": ["ES2023", "DOM", "DOM.Iterable"],
    "module": "esnext",
    "moduleResolution": "bundler",
    "types": ["vite/client"],
    "jsx": "react-jsx",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "verbatimModuleSyntax": true,
    "allowImportingTsExtensions": true,
    "noEmit": true,
    "skipLibCheck": true
  },
  "include": ["src", "vite.config.ts"]
}
vite.config.ts
import babel from "@rolldown/plugin-babel";
import react, { reactCompilerPreset } from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// React Compiler 1.0: automatic memoization at build time.
// Set REACT_COMPILER=off to compare builds and profiles without it.
const compiler = process.env.REACT_COMPILER !== "off";

export default defineConfig({
  plugins: [react(), ...(compiler ? [babel({ presets: [reactCompilerPreset()] })] : [])],
});
index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Pierogi – orders (performance)</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
src/styles.css
:root { font-family: system-ui, sans-serif; color: #1f2328; }
body { margin: 0; }
.app { max-width: 960px; margin: 0 auto; padding: 1rem; }
nav { display: flex; gap: .5rem; align-items: center; margin-bottom: 1rem; }
nav button[aria-current="true"] { font-weight: 700; }
.search { display: flex; flex-direction: column; gap: .25rem; max-width: 360px; }
.scroller { height: 480px; overflow: auto; border: 1px solid #d0d7de; border-radius: 8px; contain: strict; }
.row { display: grid; grid-template-columns: 7rem 1fr 1.5fr 6rem 6rem; gap: .5rem; align-items: center; padding: 0 .75rem; border-bottom: 1px solid #eaeef2; cursor: pointer; box-sizing: border-box; }
.row.selected { background: #edf2ff; }
.status { font-size: .8rem; }
.status-ready { color: #2b8a3e; }
.muted { color: #868e96; }
.bars { list-style: none; padding: 0; }
.bars li { display: grid; grid-template-columns: 12rem 1fr; gap: .5rem; margin: .25rem 0; }
.bar { background: #4263eb; color: #fff; padding: 0 .5rem; border-radius: 4px; }

Step 2. Move state down

The cheapest and most effective optimization needs no hook at all. If state is needed in one place only, keep it as close to that place as possible. In the example below a slow component (25 ms per render) sits next to a search field. On the left the search text lives in the parent – every keystroke also re-renders the slow menu. On the right the state moved into SearchBox. Type a few characters quickly on both sides and feel the difference.

Before and after: state too high vs state moved down – Type quickly in both fields and compare the slow component's render counters

/App.js
import { useState } from "react";
import { useRenderCount } from "./useRenderCount";

// Simulates an expensive component: ~25 ms of work per render.
function SlowMenu() {
  const renders = useRenderCount();
  const start = performance.now();
  while (performance.now() - start < 25) {
    /* busy wait */
  }
  return (
    <div className="slow">
      Slow menu (25 ms per render) <span className="renders">renders: {renders}</span>
    </div>
  );
}

/* BEFORE: the search text lives in the parent, so every keystroke re-renders SlowMenu too. */
function Before() {
  const [query, setQuery] = useState("");
  return (
    <section>
      <h3>Before: state too high</h3>
      <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Type quickly…" />
      <p>Searching for: {query || "—"}</p>
      <SlowMenu />
    </section>
  );
}

/* AFTER: the state moved down into SearchBox – the only component that needs it. */
function SearchBox() {
  const [query, setQuery] = useState("");
  return (
    <>
      <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Type quickly…" />
      <p>Searching for: {query || "—"}</p>
    </>
  );
}

function After() {
  return (
    <section>
      <h3>After: state moved down</h3>
      <SearchBox />
      <SlowMenu />
    </section>
  );
}

export default function App() {
  return (
    <div className="grid">
      <Before />
      <After />
    </div>
  );
}
/useRenderCount.js
import { useRef } from "react";

// Demo helper: counts how many times the component function ran.
// (Mutating a ref during render is not something to do in real code –
// use the React DevTools Profiler there.)
export function useRenderCount() {
  const renders = useRef(0);
  renders.current += 1;
  return renders.current;
}
/styles.css
body { font-family: system-ui, sans-serif; margin: 0; padding: 12px; color: #1f2328; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
section { border: 1px solid #d0d7de; border-radius: 8px; padding: 8px 12px; }
h3 { margin: 4px 0 8px; font-size: 16px; }
input { width: 100%; box-sizing: border-box; padding: 6px; font-size: 15px; }
.slow { margin-top: 8px; padding: 8px; background: #f1f3f5; border-radius: 6px; }
.renders { float: right; font: 12px ui-monospace, monospace; background: #fff3bf; padding: 1px 6px; border-radius: 4px; }

In our measurement 10 keystrokes produced 11 renders of the slow menu on the left (the first render plus one per keystroke) and 1 on the right. The counters work only because the hidden /index.js file renders the app without StrictMode – in development mode it doubles renders.

The rest of this article is for members

Create a free account or sign in to read it all. Still ahead:

  • Step 3. memo, useMemo, useCallback – when yes and when no
  • Step 4. React Compiler 1.0
  • Step 5. A list of 10,000 orders: useDeferredValue and memo
  • Step 6. Virtualizing the list
  • Step 7. Code splitting, Suspense and transitions
  • Common mistakes
  • Check yourself
  • What next

Create a free account · Sign in