Skip to content
Middle7 views

Why can memoization be expensive? What resources do useMemo and useCallback consume?

Practice

AI answer

useMemo and useCallback are not free: React stores the cached value or function and an array of dependencies, then compares the dependencies on every render. This requires memory and a small amount of CPU work; a closure retained by a callback can also keep objects in memory longer than expected. Evaluating dependencies and maintaining the cache also have a cost, and caching can make code more complex and cause bugs when dependencies are specified incorrectly.

Use them when they provide a measurable benefit: for example, for an expensive computation or to preserve the identity of a value/callback and avoid unnecessary work in a memoized child component. For inexpensive computations and ordinary callbacks, the overhead may outweigh the benefit. These are optimizations, not semantic guarantees.