Course contentsShow
← React
Lesson 905 of 2,39623. useMemo and useCallbackFree lesson

The Cost of Memoization Itself

Understanding that useMemo and useCallback have overhead: comparison logic, closure creation, and memory.

The Cost of Memoization Itself

You'll learn: Why useMemo and useCallback aren't free optimizations—they have their own performance cost.

When you wrap a value in useMemo or a function in useCallback, React doesn't just magically make things faster. These hooks add overhead to every render:

What Memoization Actually Does

Each time your component renders, React must:

  1. Run comparison logic - Check if dependencies changed by comparing old values to new ones
  2. Create closures - Capture variables in a new function scope
  3. Store references - Keep the memoized value in memory between renders

Think of it like hiring a librarian to track whether a book changed. If the book rarely changes, the librarian is helpful. But if you only read the book once, you've paid the librarian's salary for nothing.

The Hidden Costs

Comparison overhead: React compares every item in your dependency array using Object.is(). For an array with 5 dependencies, that's 5 comparisons on every render.

Memory cost: Memoized values stay in memory. If you memoize 20 things in a component, you're storing 20 extra references.

Closure creation: Even when returning a cached value, JavaScript still creates the wrapper function on each render.

When the Cost Outweighs the Benefit

If your calculation takes 0.1ms and memoization overhead is 0.05ms, you've only saved 0.05ms—barely measurable. Worse, if the dependencies change frequently, you pay the memoization cost and recalculate anyway.

Key Takeaway: Memoization adds its own overhead (comparisons, closures, memory). Only use it when the cost of recalculating exceeds the cost of memoizing—measure, don't guess.