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

Memoization is Not Free

Recognizing that wrapping everything in useMemo/useCallback can hurt performance more than it helps in many cases.

Memoization is Not Free

What you'll learn: Why wrapping every value and function in useMemo and useCallback can actually make your app slower, not faster.

The Hidden Cost

You've learned that memoization prevents unnecessary recalculations and re-renders. Sounds like you should wrap everything in useMemo and useCallback, right? Wrong. Memoization itself has a cost:

  1. Memory overhead: React must store the previous value and dependency array
  2. Comparison work: On every render, React compares the new dependencies to the old ones
  3. Code complexity: Your components become harder to read and maintain

For simple values or cheap calculations, this overhead costs more than just recalculating the value each time.

The Math Doesn't Always Work

Think of it like hiring a security guard to watch a penny. The guard's salary (memoization overhead) costs way more than the penny you're protecting (the simple calculation).

// BAD: This useMemo costs more than it saves
const doubled = useMemo(() => count * 2, [count]);

// GOOD: Just calculate it every time
const doubled = count * 2;

Multiplying two numbers takes nanoseconds. Storing a value, checking dependencies, and retrieving the cached result can actually take longer.

When to Memoize

Only memoize when:

  • The calculation is genuinely expensive (loops through thousands of items, complex logic)
  • The value is passed as a dependency to other hooks or memo'd components
  • Profiling shows the component has performance issues

Key Takeaway: Memoization trades memory and comparison overhead for computation savings—only use it when the computation you're avoiding costs more than the memoization machinery itself.