Lodash _.memoize() method
What you’ll learn
- How to cache expensive pure function results with
_.memoize. - How default and custom resolver keys affect cache hits.
- How to inspect, set, delete, and clear memoized cache entries.
- Common pitfalls that cause stale data or memory growth.
Prerequisites
Helpful background: _.flip(), the Function hub, and basic Map usage.
- Pure functions: same input should return the same output for safe caching.
- Cache keys: know when primitives vs object identity should define uniqueness.
Overview
_.memoize(func, [resolver]) wraps func and stores return values in an internal cache. Repeated calls with the same key return immediately without re-running expensive work.
Syntax
_.memoize(func, [resolver])- func: function whose output should be cached.
- resolver: optional function to build cache keys from arguments.
- returns: memoized wrapper with a mutable
.cacheproperty.
Basic memoization
Repeated calls with the same input hit cache and avoid expensive recomputation.
import memoize from "lodash/memoize";
let calls = 0;
const square = memoize(function (n) {
calls += 1;
return n * n;
});
square(12); // 144 (computed)
square(12); // 144 (cached)
calls; // 1Custom resolver for multiple args
When outputs depend on multiple parameters, combine them into one stable key.
import memoize from "lodash/memoize";
const sumRange = memoize(
function (start, end) {
let total = 0;
for (let i = start; i <= end; i += 1) total += i;
return total;
},
function (start, end) {
return start + ":" + end;
}
);
sumRange(1, 4); // 10
sumRange(1, 4); // from cacheInspect and clear cache
The memoized wrapper exposes a cache map-like object for lifecycle control.
import memoize from "lodash/memoize";
const upper = memoize((text) => text.toUpperCase());
upper("hello");
upper.cache.has("hello"); // true
upper.cache.clear();
upper.cache.has("hello"); // false📋 _.memoize vs manual Map cache
| Approach | Strength | Trade-off |
|---|---|---|
_.memoize | Quick wrapper API with built-in cache holder | Default key is first arg; resolver needed for multi-arg precision |
Manual Map | Total control over eviction and key design | More boilerplate and repeated patterns |
Pitfalls to avoid
Caching side effects
Memoize is best for deterministic computations, not functions with changing external state.
Wrong cache key strategy
Without a resolver, calls differing only in later args can collide under the same first-arg key.
Never-cleared caches
Long-lived processes should clear stale memoized entries or adopt bounded caching.
❓ FAQ
Summary
- Purpose:
_.memoizecaches function outputs by key to skip repeated computation. - Control: use resolvers for accurate keys and manage
memoized.cacheas needed. - Next: Lodash _.negate(), revisit Lodash _.flip(), or read official _.memoize docs.
Lodash memoization stores results in a MapCache at memoized.cache, and by default uses only the first argument as the cache key unless you supply a resolver.
6 people found this page helpful
