Skip to content

Performance Optimization

Identifying and fixing React performance bottlenecks with profiling and optimization techniques.

What you'll learn

  • Using the React Profiler to identify bottlenecks
  • Reducing unnecessary re-renders
  • Virtualizing long lists for performance
  • Debouncing, throttling, and deferring work

Concept

Identify Before You Optimize

The first rule of performance optimization is measure before you optimize. React DevTools Profiler shows you exactly which components re-render, how long each render takes, and what caused the re-render. Record a profile while interacting with your app, look for components that re-render unnecessarily or take a long time, and focus your optimization efforts there. Do not guess — profile and let the data guide you.

Reducing Re-renders

Unnecessary re-renders are the most common React performance issue. A parent re-rendering causes all children to re-render by default, even if their props have not changed. Fix this with React.memo for expensive children, useMemo and useCallback to stabilize props, and by keeping state as local as possible so that updates do not cascade through the tree. Split large components into smaller ones so that a state change in one part does not re-render the entire subtree.

Virtualizing Long Lists

Rendering thousands of DOM nodes is slow — the browser struggles to lay out and paint them all. Virtualization (or windowing) renders only the visible items plus a small buffer, replacing the rest with empty space. As the user scrolls, the items are recycled. Libraries like react-window and @tanstack/react-virtual handle this efficiently. For any list with more than a few hundred items, virtualization is essential for smooth scrolling.

Debouncing and Throttling

Some events fire very frequently: scroll, resize, mousemove, and text input. If your handler does expensive work on every event, the UI will lag. Debouncing delays the handler until the events stop coming for a specified delay (useful for search input). Throttling limits the handler to fire at most once per specified interval (useful for scroll handlers). Use the useDeferredValue hook (React 18) to defer expensive rendering without blocking input, and startTransition to mark non-urgent state updates.

Example

              import { useState, useMemo, useDeferredValue, memo } from "react";

// Memoized row — only re-renders when its data changes.
const Row = memo(function Row({ item }) {
  return <tr><td>{item.id}</td><td>{item.name}</td><td>{item.value}</td></tr>;
});

function SearchableTable({ items }) {
  const [query, setQuery] = useState("");
  // useDeferredValue lets the input stay responsive while filtering lags behind.
  const deferredQuery = useDeferredValue(query);
  const isStale = query !== deferredQuery;

  // Memoized filtering — only recomputes when items or deferredQuery change.
  const filtered = useMemo(
    () => items.filter(i => i.name.toLowerCase().includes(deferredQuery.toLowerCase())),
    [items, deferredQuery]
  );

  return (
    <div>
      <input
        value={query}
        onChange={e => setQuery(e.target.value)}
        placeholder="Search..."
        style={{ opacity: isStale ? 0.7 : 1 }}
      />
      <table>
        <tbody>
          {filtered.map(item => <Row key={item.id} item={item} />)}
        </tbody>
      </table>
    </div>
  );
}
            

useDeferredValue keeps the input responsive while the expensive filter catches up. useMemo caches the filtered list. memo prevents Row re-renders when only the query changes.

Try it

  • Code Diff

    Compare optimized and unoptimized component versions.

  • Code Minifier

    Minified code runs faster — see the impact on your bundle.

Common mistakes

The mistake

Optimizing prematurely without profiling first

The fix

Use the React DevTools Profiler to identify actual bottlenecks before adding memo, useMemo, or useCallback. Premature optimization adds complexity and can even slow things down with unnecessary comparisons.

The mistake

Rendering thousands of list items without virtualization

The fix

For lists with more than a few hundred items, use virtualization (react-window or @tanstack/react-virtual). Rendering all items creates thousands of DOM nodes that the browser cannot handle smoothly.

Practice Performance Optimization

1 exercise

Practice Performance Optimization

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors