useMemo Memoized Computation

Last updated: 2026-11-02

useMemo Memoized Computation

useMemo caches the result of a computation: while the dependencies stay the same it returns the previous result and skips the expensive filtering, sorting or mapping, and it also keeps the reference of an object or array stable, which pairs with a dependency array and memoized child components. It is a performance tool, not a semantic guarantee: React is free to discard the cache, so the result must stay correct even when the memo is recomputed from scratch.

GroupEffects & Performance
KindHook
SinceReact 16.8

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

const result = useMemo(() => {
  return computeHeavy(list, keyword)   // the expensive computation
}, [list, keyword])                    // no recomputation while the deps stay the same

⚙️ Parameters

factoryA function without arguments that returns the value to cache. It only runs again when the dependencies change.
depsThe dependency array: any change recomputes the value; an empty array computes it only on mount.

Returns:Returns the cached result of the factory; while the dependencies do not change it returns the very same reference as last time.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Typing a keyword filters the list in real time; clicking "Toggle theme" only swaps the background color and does not filter again (the useMemo cache is doing its job).

💡 Key Points

💡 Tip Two main uses: caching expensive computations and keeping references stable. The latter is the precondition for the "object dependency + memoized child" combination to hold.
⚠️ Warning Wrapping a trivial computation such as adding two numbers in useMemo makes things slower and harder to read — measure the slowdown first, then optimize.
ℹ️ Info The cache may be thrown away by React (for instance when an off-screen component remounts), so do not treat useMemo as a guarantee that the value stays cached.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between useMemo and useCallback?
A useMemo caches a computed value, useCallback caches a function reference; the latter is equivalent to useMemo(() => fn, deps).
Q Is an empty dependency array safe?
A Only when the factory reads no reactive value from the outside; reading state or props without listing them yields a stale result.
Q Will wrapping every computation in useMemo make things faster?
A No. Caching has memory and comparison costs, so memoizing simple computations is a pessimization — use it for real hot spots only.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏