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

Identifying Expensive Computations in Render

Learn to recognize computations that actually slow down renders: complex algorithms, large array operations, and heavy transformations.

Identifying Expensive Computations in Render

What you'll learn: How to recognize which computations in your render function are actually slow enough to benefit from memoization.

The Problem: Not All Code is Expensive

When you're deciding whether to use useMemo, you first need to identify whether a computation is actually expensive. Most JavaScript operations are incredibly fast—modern browsers can perform millions of simple calculations per second. The challenge is distinguishing between code that looks complex and code that genuinely slows down your app.

What Makes a Computation Expensive?

Think of your CPU like a factory worker. Adding two numbers? That's like putting a label on a box—takes milliseconds. But these operations are like moving entire pallets:

Complex algorithms:

  • Recursive calculations (fibonacci, tree traversals)
  • Sorting or searching through large datasets
  • Mathematical operations with many iterations
  • String manipulation on very long text

Large array operations:

  • .map(), .filter(), or .reduce() on arrays with thousands of items
  • Multiple chained array methods (each creates a new array)
  • Operations that touch every element repeatedly

Heavy transformations:

  • Converting between data formats (e.g., nested arrays to objects)
  • Deep cloning large objects
  • Formatting or parsing complex data structures

What's Usually Fast Enough?

  • Simple math (price * quantity)
  • Array operations on small lists (< 100 items)
  • String concatenation
  • Object property access
  • Most conditional logic

How to Actually Check

Before reaching for useMemo, use the React DevTools Profiler to measure render time. If a component renders in under 1-2 milliseconds, the computation isn't your bottleneck—even if it "looks" complicated.

Key Takeaway: Expensive computations involve processing large amounts of data or running complex algorithms; most everyday JavaScript operations are fast enough that memoization adds overhead without benefit.