Lodash _.memoize() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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

javascript
_.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 .cache property.
1

Basic memoization

Repeated calls with the same input hit cache and avoid expensive recomputation.

javascript
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;      // 1
2

Custom resolver for multiple args

When outputs depend on multiple parameters, combine them into one stable key.

javascript
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 cache
3

Inspect and clear cache

The memoized wrapper exposes a cache map-like object for lifecycle control.

javascript
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

ApproachStrengthTrade-off
_.memoizeQuick wrapper API with built-in cache holderDefault key is first arg; resolver needed for multi-arg precision
Manual MapTotal control over eviction and key designMore boilerplate and repeated patterns

Pitfalls to avoid

Purity

Caching side effects

Memoize is best for deterministic computations, not functions with changing external state.

Keys

Wrong cache key strategy

Without a resolver, calls differing only in later args can collide under the same first-arg key.

Memory

Never-cleared caches

Long-lived processes should clear stale memoized entries or adopt bounded caching.

❓ FAQ

It returns a wrapped function that caches output values, so repeated calls with the same key return quickly from cache.
By default, lodash memoize uses the first argument as the cache key.
Use a resolver when your function depends on multiple arguments or object inputs and you need deterministic cache keys.
Use memoized.cache.clear() to remove all entries, or set/delete specific keys on memoized.cache.
Yes. Unbounded key growth can increase memory usage, so clear old entries or use bounded caching strategies.

Summary

Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful