Underscore.js _.memoize() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Memoizes a function by caching computed results — useful for expensive or repeated work. An optional hashFunction builds the cache key from the arguments (official Underscore docs). Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.memoize. Start with this line.

Underscore
_.memoize(fn, [hashFunction])
CallWhat it does
_.memoize(fn, [hashFunction])Primary signature.
_.memoize(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.memoize(fn, [hashFunction])

Parameters

ParameterDescription
functionRequired. Function to wrap
hashFunctionOptional. Builds cache key from arguments

Return Value

Return ValueDescription
*Memoized function (+ .cache)

Underscore.js Examples

Example 1 — Cache a Result

The inner function runs once per unique input.

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

square(5);
square(5);
square(5);
// → 25, calls === 1
Try It Yourself

How It Works

  1. Wrap. _.memoize returns a caching wrapper.
  2. First 5. Runs the body; stores 25.
  3. Next calls. Same key → return from cache.
  4. Counter. calls stays at 1.

Example 2 — Memoized Fibonacci

The classic docs example — recursion with reuse.

Underscore
var fibonacci = _.memoize(function (n) {
  return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});

fibonacci(10);
// → 55
Try It Yourself

How It Works

  1. Assign to a name. Recursive calls must hit the memoized function.
  2. Base case. n < 2 returns n.
  3. Reuse. Each fib(k) is stored after the first compute.
  4. Speed. Overlapping subproblems stop exploding.

Example 3 — Custom Hash Function

When results depend on more than the first argument, build a key.

Underscore
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)
Try It Yourself

How It Works

  1. Default pitfall. Without a hash, both would key as 2.
  2. Join args. "2,3" vs "2,4" are distinct.
  3. Correct cache. Each pair stores its own sum.
  4. Rule. Hash must uniquely identify the inputs that matter.

Example 4 — Inspect .cache

The memoized function exposes stored results as a plain object.

Underscore
var double = _.memoize(function (n) {
  return n * 2;
});

double(2);
double(3);
double.cache;
// → { "2": 4, "3": 6 }

// clear everything:
// double.cache = {};
Try It Yourself

How It Works

  1. Property. Docs: cache is on the returned function.
  2. Keys. String forms of the cache keys (here "2", "3").
  3. Values. Previously computed return values.
  4. Reset. Assign {} when you need a fresh start.

Browser & Runtime Support

_.memoize ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.memoize()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative: a Map/object wrapper that stores results by a key you define.

Frequently Asked Questions

_.memoize(fn, [hashFunction]) returns a cached version of fn. The first call with a given key runs the function; later calls with the same key return the stored result.
By default Underscore uses the first argument as the key. Pass a hashFunction when the result depends on more than one argument.
Yes. The returned function has a .cache object. Read it to inspect values, or set memoized.cache = {} to clear.
Avoid it for impure functions, results that change over time, or huge unique-argument spaces that would grow the cache forever.
Yes. Memoizing Fibonacci (as in the official docs) turns exponential overlapping work into linear cached work.
No. It is a stable Underscore Function helper.

Next: _.delay()

Call a function after a given number of milliseconds.

_.delay() →

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