This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
If a function always returns the same output for the same input, cache the result so you never compute it twice. That's memoization — trading memory for speed.
Click a compute button. The first call takes a full second. Click it again — instant. The cache remembers.
Same idea, applied to components. React.memo skips re-rendering a child when its props haven't changed. useCallback keeps function references stable so memo can do its job.
Without memo, every parent re-render cascades to all children — even if their props are identical.
memoize()
function memoize(fn) {const cache = new Map()return (...args) => {const key = JSON.stringify(args)if (cache.has(key)) return cache.get(key)const result = fn(...args)cache.set(key, result)return result}}
O(1) lookup, O(n) space for n unique inputs