Lodash _.memoize() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Creates a function that caches the result of func. Later calls with the same cache key return the stored value instead of re-running the work. Part of Lodash 4.x Function (since 0.1.0).

Pass the required arguments shown below. Most helpers return a new function you call later.

Syntax

Call _.memoize. Start with this line.

Lodash
_.memoize(func, [resolver])
CallWhat it does
_.memoize(func, [resolver])Primary signature.
_.memoize(func)Wrap or transform a function.

Prefer import memoize from "lodash/memoize" when you want a smaller bundle.

Library declarations
Lodash
_.memoize(func, [resolver])

Parameters

ParameterDescription
funcRequired. Function whose results are cached
resolverOptional. Builds the cache key from arguments

Return Value

Return ValueDescription
*Memoized function with .cache

Lodash Examples

Example 1 — Official: Memoize _.values

Different object keys store different results.

Lodash
var object = { a: 1, b: 2 };
var other  = { c: 3, d: 4 };
var values = _.memoize(_.values);

values(object);
// → [1, 2]

values(other);
// → [3, 4]
Try It Yourself

How It Works

  1. Wrap. _.memoize(_.values) returns a caching wrapper.
  2. First key. object is the Map key → run and store [1, 2].
  3. Second key. other is a different reference → store [3, 4].
  4. Reuse. Calling again with the same object skips _.values.

Example 2 — Official: Mutation Does Not Bust Cache

Changing properties on the same object still hits the old result.

Lodash
var object = { a: 1, b: 2 };
var values = _.memoize(_.values);

values(object);
// → [1, 2]

object.a = 2;
values(object);
// → [1, 2]   // cached — not [2, 2]
Try It Yourself

How It Works

  1. Key. Cache keys by object identity, not deep content.
  2. Mutate. object.a = 2 changes data, not the key.
  3. Hit. Second call returns the first stored array.
  4. Fix. Clear the entry or use a resolver that fingerprints content.

Example 3 — Official: Override with cache.set

Write a value directly into the memoized function’s cache.

Lodash
var object = { a: 1, b: 2 };
var values = _.memoize(_.values);

values(object);
// → [1, 2]

values.cache.set(object, ["a", "b"]);
values(object);
// → ["a", "b"]
Try It Yourself

How It Works

  1. Expose. values.cache is the live Map-like store.
  2. Set. Replace the stored result for object.
  3. Next call. Returns the manually set value.
  4. Also. You can delete, has, get, or clear.

Example 4 — Resolver for Multiple Arguments

Without a resolver, only the first arg is the key — use a resolver for both.

Lodash
var add = _.memoize(
  function (a, b) {
    return a + b;
  },
  function (a, b) {
    return a + ":" + b;
  }
);

add(2, 3);
// → 5

add(2, 4);
// → 6   // different key "2:4"

add(2, 3);
// → 5   // cache hit for "2:3"
Try It Yourself

How It Works

  1. Problem. Default key would be only a — add(2,3) and add(2,4) would collide.
  2. Resolver. Builds "2:3" / "2:4" from both args.
  3. Misses. First two calls compute and store.
  4. Hit. Third call reuses "2:3".

Example 5 — Clear the Cache

Drop all entries when results are no longer valid.

Lodash
var calls = 0;
var square = _.memoize(function (n) {
  calls += 1;
  return n * n;
});

square(4);
// → 16, calls = 1

square(4);
// → 16, calls = 1   // cache hit

square.cache.clear();
square(4);
// → 16, calls = 2   // recomputed
Try It Yourself

How It Works

  1. Counter. Tracks how often the real function runs.
  2. Hit. Second square(4) does not increment calls.
  3. Clear. Empties every cached key.
  4. Miss again. Next call recomputes and bumps calls to 2.

Browser & Runtime Support

_.memoize is a Lodash 4.x Function helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. There is no single native Function.memoize; Lodash provides a portable Map-style cache with an optional resolver.

✓ Stable · Lodash 4.x

_.memoize()

Available in [email protected] full builds and the lodash/memoize modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.memoize Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import memoize from "lodash/memoize" for smaller builds. Ideal for expensive pure lookups, selectors, and API response shaping by key.

Frequently Asked Questions

_.memoize(func, [resolver]) returns a wrapped function that caches results. Repeated calls with the same cache key return the stored value without re-running func.
The first argument only. Extra arguments are ignored for the key unless you pass a resolver.
An optional function that receives the same arguments as the memoized call and returns the cache key to use.
Call memoized.cache.clear(), or use cache.delete(key) / cache.set(key, value) for individual entries.
once remembers one result forever (ignores later args). memoize caches many results keyed by argument.
Use import memoize from "lodash/memoize" or require("lodash/memoize") for tree-shaking. Full build: import _ from "lodash" then _.memoize.

Next: _.negate()

Wrap a predicate so it returns the opposite boolean result.

_.negate() tutorial →

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.

10 people found this page helpful