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

When Memoization Actually Runs

How React compares dependency array values with Object.is() to decide whether to recompute or return cached result.

When Memoization Actually Runs

What you'll learn: How React decides whether to recompute a memoized value or return the cached one.


The Comparison Mechanism

When you use useMemo or useCallback, React doesn't just trust that your dependencies haven't changed. On every render, React performs a comparison check:

  1. It looks at each value in your dependency array
  2. It compares the current value with the previous value using Object.is()
  3. If any dependency has changed, React runs your function again
  4. If all dependencies are unchanged, React returns the cached result

What is Object.is()?

Object.is() is JavaScript's strictest equality check. It's like ===, but with two special cases (not common in React):

Object.is(5, 5)           // true
Object.is('hello', 'hello') // true
Object.is({}, {})          // false (different object references)
Object.is(arr, arr)        // true (same reference)

The critical point: object and array comparisons check reference equality, not deep content equality.

What This Means for You

Your memoized function runs again when:

  • A primitive dependency changes value (5 → 6)
  • An object/array dependency gets a new reference, even if the contents are identical
  • The dependency array is missing or empty (special rules apply)

Your cached result is returned when:

  • All primitives still have the same value
  • All objects/arrays still point to the same reference

Key Takeaway: React's memoization comparison is shallow and reference-based using Object.is(). This is why creating new objects or arrays inline in dependency arrays defeats memoization—React sees them as different every time, even if their contents are identical.