Course contentsShow
← React
Lesson 2279 of 2,39650. Performance: Optimization TechniquesFree lesson

The Cost of useMemo Itself

Understand that memoization adds overhead: comparing dependencies and storing results.

The Cost of useMemo Itself

What you'll learn: Memoization with useMemo isn't free—it adds overhead that can sometimes outweigh its benefits.

The Hidden Price of Optimization

When you wrap a value in useMemo, React doesn't just magically make things faster. Behind the scenes, it performs two operations on every render:

  1. Dependency comparison: React checks if any of the dependencies in the array have changed (using shallow equality)
  2. Storage overhead: React must store both the memoized value and the dependency array in memory

Think of it like hiring a security guard to protect a bicycle. The guard costs money, takes up space, and needs to constantly check their watch list. If the bicycle is cheap and rarely stolen, the guard's salary exceeds the value they provide.

When the Cost Exceeds the Benefit

// This useMemo probably costs MORE than it saves
const doubledValue = useMemo(() => count * 2, [count]);

Simple calculations (like multiplication) execute in microseconds. The dependency comparison and memory storage can actually take longer than just recalculating the value each time.

What Actually Costs Performance

useMemo helps when:

  • The computation is genuinely expensive (complex calculations, array transformations)
  • The value is used as a prop to a memoized component
  • The value is a dependency in other hooks

It doesn't help when the calculation is trivial—you're just trading one cheap operation for a slightly more expensive one.

Key Takeaway: Always profile before adding useMemo. The memoization overhead (dependency checking + storage) must cost less than the computation you're trying to avoid—otherwise you've made performance worse, not better.