DevPath · Learn to code ESPTEN

Custom hooks and performance

When (and when not) to optimize

Premature optimization is a cost

React.memo, useMemo and useCallback are not free: each one stores values in memory and compares dependencies on every render. If you apply them to everything "just in case", you add complexity and sometimes even worsen the performance.

"Premature optimization is the root of all evil." — Donald Knuth

Measure first

The golden rule: measure before optimizing. Don't optimize based on hunches. Tools to measure:

When SHOULD you optimize?

When NOT?

Better than memoizing: reduce the work

Often the best optimization is not memo, but changing the structure:

Correct order: 1) write clear code, 2) measure if there's a real problem, 3) optimize only what the Profiler points out, 4) measure again to confirm the improvement.

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Performance: why React re-rendersView the module →