Memoize
JS Functions
Medium
15 min
closures
caching
performance
Implement memoize(func, resolver?). It returns a function that caches func's results. If the memoized function is called again with an input it has already seen, it returns the cached value instead of calling func.
const slowSquare = (n) => {
/* imagine something expensive */
return n * n;
};
const square = memoize(slowSquare);
square(4); // 16, computed
square(4); // 16, from the cache
square(5); // 25, computed
const byId = memoize(
(user) => fetchProfile(user.id),
(user) => user.id // resolver: decides the cache key
);
Requirements
- Cache key: by default, the first argument only. If
resolveris given, the key isresolver(...args), called with the samethisas the memoized function. - Keys are compared with SameValueZero, the same comparison
Mapuses:1and'1'are different keys.- Objects are compared by reference.
NaNmatchesNaN.
- Results are cached even when they are
undefined. A cache hit must not callfunc. - If
functhrows, nothing is cached and the error propagates, so the next call with that key tries again. funcis called with all of the original arguments and the originalthis.- The memoized function exposes its cache as
memoized.cache, aMapfrom key to result, so callers can inspect it,deleteentries orclear()it.
Follow-up
- When does memoization leak memory, and how would you bound the cache (LRU, TTL,
WeakMap)? - How would you memoize a function that takes several arguments? (See Memoize II.)
- How would you memoize an async function so that concurrent calls share one request?
Spotted a mistake or a rights concern? Report an issue