← Back to Roadmap

Memoization

Medium

In plain terms

Cache function results for same inputs. Avoid redundant computation. Use Map or object as cache. Common for recursive functions.

What you need to know

  • Cache results
  • Same input = cached output
  • Performance optimization

Try it yourself

Copy the code below and run it in your browser console or a code editor:

const memo = (fn) => { const cache = new Map(); return (n) => cache.has(n) ? cache.get(n) : cache.set(n, fn(n)).get(n); };

Learn more

Dive deeper with these trusted resources: