JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsreactPerformance Optimization
PrevNext
react
advanced
12 min read

Performance Optimization

memo
optimization
performance
profiler
react-compiler
useCallback
useMemo

React.memo skips re-renders when props haven't changed, useMemo caches computed values, and useCallback caches function references — but these are targeted optimizations, not defaults for every component.

Key Points

1React.memo

HOC that skips re-renders when props haven't changed (shallow comparison). Only effective when all props are referentially stable.

2useMemo and useCallback

useMemo caches computed values, useCallback caches function references — both keep prop references stable for memoized children.

3Profile Before Optimizing

Use React DevTools Profiler to identify actual bottlenecks. Memoization adds complexity — don't apply it everywhere by default.

4The Three Work Together

React.memo on the child + useMemo/useCallback on the props passed to it = effective memoization. One without the other often has no effect.

5React Compiler

React 19's compiler auto-memoizes at build time — may eliminate the need for manual React.memo, useMemo, and useCallback.

What You'll Learn

  • Explain React.memo, useMemo, and useCallback and when each should be used
  • Know why memoization requires all three tools working together
  • Profile React applications to identify actual performance bottlenecks

Deep Dive

React re-renders a component whenever its state changes or its parent re-renders. Most of the time this is fast enough, but for expensive components or large lists, unnecessary re-renders cause visible lag. React provides three memoization tools to address this.

React.memo

A higher-order component that wraps a functional component and skips re-rendering when all props are the same (shallow comparison). const MemoizedList = React.memo(List) — if the parent re-renders but List's props haven't changed, React reuses the previous output without calling the component function.

Critical caveat: React.memo only works if all props are referentially stable. If the parent creates new objects or functions on each render (<List items={data.filter(x => x.active)} onClick={() => handleClick()} />), the props are different every time and memo has no effect. This is where useMemo and useCallback become relevant.

useMemo

const filtered = useMemo(() => data.filter(x => x.active), [data]) caches the computed value between renders, only recomputing when dependencies change. Without useMemo, the filter runs on every render and produces a new array reference, breaking React.memo.

Use cases: expensive computations (sorting, filtering large lists), creating objects/arrays passed to memoized children, complex derived values.

useCallback

const handleClick = useCallback(() => { ... }, [deps]) caches the function reference between renders. It's equivalent to useMemo(() => fn, [deps]). Without it, () => { ... } creates a new function on every render, breaking React.memo on children that receive it as a prop.

Use cases: event handlers passed to memoized children, functions used in useEffect dependency arrays, callbacks passed to expensive child components.

When NOT to Optimize

Memoization adds complexity and memory overhead. Don't use it unless you've identified a performance problem:

  • Don't memo every component by default — most components render fast enough without it
  • Don't useMemo simple calculations (a + b) — the memoization overhead exceeds the computation cost
  • Don't useCallback every handler — only matters when passed to memoized children
  • Measure first with React DevTools Profiler — it shows which components re-render and how long they take

React DevTools Profiler

The Profiler tab in React DevTools records rendering performance. It shows which components rendered, how long each took, and why they re-rendered (props changed, state changed, parent rendered). This is the essential tool for identifying optimization targets.

React Compiler (React 19)

The React Compiler automatically memoizes components and values at build time, potentially eliminating the need for manual React.memo, useMemo, and useCallback. As of 2025, it's production-ready at Meta and being adopted by the wider ecosystem. It analyzes component code and inserts memoization where beneficial.

Key Interview Distinction

React.memo prevents child re-renders when props are unchanged. useMemo caches values to keep prop references stable. useCallback caches functions for the same purpose. These three work together — memo on the child, useMemo/useCallback on the props. But don't optimize blindly — profile first, optimize specific bottlenecks, and know that the React Compiler may automate this entirely.

Fun Fact

Dan Abramov (React core team) famously wrote 'Before You memo()' arguing that restructuring components (moving state down, lifting content up) is often more effective than memoization. The React Compiler embodies this philosophy — it analyzes your code structure and applies optimizations automatically.

Learn These First

Hooks

intermediate

State

beginner

Continue Learning

Hooks

intermediate

React Internals

advanced

Concurrent React

advanced

Practice What You Learned

Explain React.memo, useMemo, and useCallback. When should each be used?
senior
performance
React.memo prevents component re-renders when props are unchanged. useMemo caches computed values. useCallback caches function references. Use them only when you have measured performance issues - premature optimization adds complexity without benefit.
How does list virtualization (windowing) work in React and when should you use it?
senior
performance
List virtualization renders only the items currently visible in the viewport plus a small overscan buffer, replacing thousands of DOM nodes with a handful. This dramatically reduces initial render time, memory usage, and DOM size for large lists.
Previous
Advanced Patterns
Next
Props
PrevNext