Premature Optimization — Root of All Evil

Many beginners, learning about memo and useMemo, start wrapping every piece of code in them. This is a dangerous path that can make your application slower.

1. The Price of " Free " Optimization

Memoization is not free magic. Every time you use useMemo:

  1. Memory is spent: React must store the old value and old dependency array.
  2. Processor time is spent: On every render, React must iterate through the dependency array and compare each value with the previous one. If you cache simple addition of two numbers, caching will take more time than the calculation!

2. Checklist: When SHOULD You Optimize?

Don't guess, check. Use optimization if:

  • Visual lags: You type in a search field, but text appears with delay (freezes).
  • Huge lists: You have more than 500 elements, and they're often redrawn entirely.
  • Heavy math: Filtering thousands of objects, generating charts, or text processing.
  • Passing functions down: When it's critically important to maintain a stable reference for props going into a very heavy React.memo component.

3. First — Profiler

In React DevTools there's a Profiler tab.

  1. Enable recording.
  2. Use the application.
  3. Look at the report. The system will highlight " hot " components that render too long. Fix only them.

4. Golden Rule: Restructuring > Memoization

Often lags are solved not through useMemo, but through proper structure.

  • Example: If a " heavy " component redraws because you're moving the mouse in the parent — just move the mouse state to a separate small component. Then the heavy component won't even know about mouse movement and won't redraw.

Remember: Clean and understandable code is more important than micro-optimizations users don't notice.