Comprehensive guide to performance optimization: when, why, and how to reuse computation results and references.
Memoization is saving the result of a function execution to prevent re-computation given the same input parameters.
Goal: Maintain reference stability and cache heavy computations to minimize redundant re-renders.
Memoization of a computation result. Used for heavy calculations or creating stable object/array references.
1const value = useMemo(() => compute(a, b), [a, b]);Memoization of a function reference. Essential when passing functions to memoized components or hook dependencies.
1const handler = useCallback(() => { ... }, [dep]);Preventing redundant component re-renders. Wraps a component for shallow prop comparison.
1export const MyComp = memo(({ props }) => { ... });Passing stable references (functions, objects) to child memo components.
Stabilizing dependencies for useEffect, useMemo, and other hooks.
Caching results of filtering, sorting, or complex data transformations.
Memoization is not free. It consumes additional memory and CPU resources for dependency comparison.