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

When Memoization Helps: Long Lists and Expensive Filters

Profile real scenarios where useMemo prevents recalculating filtered/sorted large datasets.

When Memoization Helps: Long Lists and Expensive Filters

What you'll learn: How to identify real scenarios where useMemo prevents recalculating filtered or sorted large datasets on every render.

The Real-World Problem

You've learned when not to memoize—but when should you? Two classic scenarios emerge from profiling: filtering large lists and expensive sorting operations. If you're rendering 10,000 items and filtering them by search input, recalculating that filtered array on every parent re-render (even when the search term hasn't changed) wastes precious milliseconds.

Where Memoization Pays Off

Large Dataset Filtering

When you have hundreds or thousands of items and filter them based on user input:

// Without useMemo: recalculates every render
const filtered = items.filter(item => 
  item.name.toLowerCase().includes(search.toLowerCase())
);

// With useMemo: only recalculates when items or search changes
const filtered = useMemo(
  () => items.filter(item => 
    item.name.toLowerCase().includes(search.toLowerCase())
  ),
  [items, search]
);

Expensive Sort Operations

Sorting 1,000+ items by multiple criteria creates measurable delay:

const sorted = useMemo(
  () => [...items].sort((a, b) => {
    // Complex multi-field comparison
    return a.priority - b.priority || a.date - b.date;
  }),
  [items]
);

Profile to Confirm

Always use the Profiler to verify the computation is actually expensive. If the Flamegraph shows that filter/sort step taking >5ms and it's happening unnecessarily, useMemo is justified. If it's <1ms, the memoization overhead might cost more than it saves.

Key Takeaway: useMemo shines when profiling reveals that filtering/sorting large arrays (500+ items) creates measurable render delays, and those operations depend on props or state that change less frequently than the parent component renders.