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])
Call
What 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
Parameter
Description
function
Required. Function to wrap
hashFunction
Optional. Builds cache key from arguments
Return Value
Return Value
Description
*
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
Property. Docs: cache is on the returned function.
Keys. String forms of the cache keys (here "2", "3").
Values. Previously computed return values.
Reset. Assign {} when you need a fresh start.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.memoizeUnderscore 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.