Course contentsShow
← React
Lesson 671 of 2,39618. When NOT to Use useEffectFree lesson

Expensive Computations: When to Use useMemo

Identifying truly expensive derived calculations that benefit from memoization instead of recomputing.

Expensive Computations: When to Use useMemo

What you'll learn: How to identify which derived calculations are actually expensive enough to justify memoization with useMemo.

The Core Idea

Most computations in React are incredibly fast—filtering arrays, simple math, string operations. These run in microseconds and happen so quickly that wrapping them in useMemo actually adds overhead rather than improving performance. However, some calculations are genuinely expensive: processing thousands of items, complex mathematical operations, deep object transformations. For these rare cases, useMemo prevents unnecessary recomputation on every render.

When Is a Computation "Expensive"?

Think of it like cooking: chopping an onion (filtering 10 items) takes seconds—you don't need a meal prep service. But preparing a five-course dinner for 20 people (processing 10,000 items with nested loops) takes hours—here, having pre-prepped ingredients makes sense.

Signs of expensive computation:

  • Processing arrays with thousands of items
  • Nested loops or recursive algorithms
  • Heavy string parsing or regex operations
  • Complex mathematical calculations
  • Operations you can feel lag in the UI

Not expensive (don't use useMemo):

  • Filtering/mapping small arrays (<100 items)
  • Simple arithmetic or string concatenation
  • Single-level object transformations
  • Most everyday calculations

The Rule of Thumb

Start without useMemo. Only add it when:

  1. You notice visible performance issues
  2. Profiling shows this specific calculation is the bottleneck
  3. The computation runs on every render and takes >10-20ms

Premature optimization with useMemo everywhere makes code harder to read and debug without real benefits.

Key Takeaway: Only use useMemo for genuinely expensive computations that you've identified as performance bottlenecks—most derived values should be computed directly during render without memoization.