Ramda.js R.memoizeWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Takes a string-returning keyGen and a function fn, then returns a new function that caches results for later calls with the same key (official Ramda docs). On each call, keyGen runs on the same arguments; if that key is already stored, fn is skipped.

Stable since v0.24.0: R.memoizeWith is a core Ramda helper — not deprecated, experimental, or non-standard. It replaced the removed R.memoize. You always supply the key function yourself.

Syntax

Call R.memoizeWith. Start with this line.

Ramda
R.memoizeWith(keyGen, fn)
CallWhat it does
R.memoizeWith(keyGen, fn)Primary signature.
R.memoizeWith(...)Call the function helper.

Prefer import { memoizeWith } from "ramda" when you want a named import.

Library declarations
Ramda
R.memoizeWith(keyGen, fn)

Parameters

ParameterDescription
keyGenRequired. Turns args into a cache key (string)
fnRequired. Function whose results are cached

Return Value

Return ValueDescription
*Memoized function with the same arity

Ramda Examples

Example 1 — Official Age Cache

Key on birth/death. First call runs fn; same dates return the cache.

Ramda
const withAge = R.memoizeWith(
  o => `${o.birth}/${o.death}`,
  ({ birth, death }) => {
    console.log(`computing age for ${birth}/${death}`);
    return { birth, death, age: death - birth };
  }
);

console.log(JSON.stringify(withAge({ birth: 1921, death: 1999 })));
console.log(JSON.stringify(withAge({ birth: 1921, death: 1999 })));
Try It Yourself

How It Works

  1. Key from fields. keyGen builds the string '1921/1999' from birth and death.
  2. First miss. Cache is empty, so fn runs, logs, and stores age: 78.
  3. Second hit. A new object with the same dates still maps to '1921/1999' — fn is skipped.
  4. Docs intent. Cache on the fields that matter, not object identity.

Example 2 — Factorial Call Count

R.identity is a fine key for a single number. A counter proves the second 5 is a hit.

Ramda
let count = 0;
const factorial = R.memoizeWith(R.identity, n => {
  count += 1;
  return R.product(R.range(1, n + 1));
});

console.log(factorial(5));
console.log(factorial(5));
console.log(factorial(6));
console.log(count);
Try It Yourself

How It Works

  1. Key is n. R.identity returns the number itself as the cache key.
  2. First 5. R.range(1, 6) → product 120; count becomes 1.
  3. Second 5. Same key → cached 120; count stays 1.
  4. New 6. Fresh key → product 720; count becomes 2 (not 3).

Example 3 — Identity Collision

R.identity keeps only the first argument — sum(1, 2) and sum(1, 9) collide.

Ramda
let count = 0;
const sum = R.memoizeWith(R.identity, (x, y) => {
  count += 1;
  return x + y;
});

console.log(sum(1, 2));
console.log(sum(1, 9));
console.log(count);
Try It Yourself

How It Works

  1. First call. Key is 1; fn returns 3.
  2. Wrong reuse. sum(1, 9) also keys as 1, so the cache returns 3 instead of 10.
  3. One run. count stays 1 — proof that fn never saw the second pair.
  4. Fix. Include every argument in the key (see Example 4).

Example 4 — Join Every Argument

R.unapply(R.join(':')) turns (1, 2) into '1:2' and (1, 9) into '1:9'.

Ramda
let count = 0;
const sum = R.memoizeWith(R.unapply(R.join(':')), (x, y) => {
  count += 1;
  return x + y;
});

console.log(sum(1, 2));
console.log(sum(1, 9));
console.log(sum(1, 2));
console.log(count);
Try It Yourself

How It Works

  1. Collect args. R.unapply turns (x, y) into [x, y].
  2. Join key. R.join(':') makes '1:2' vs '1:9'.
  3. Distinct results. First call → 3; second → 10; third → cached 3.
  4. Separator tip. Pick a delimiter that cannot appear in the values.

Example 5 — R.toString for Lists

Two equal arrays are different references. R.toString still gives them the same key.

Ramda
let count = 0;
const sumList = R.memoizeWith(R.toString, xs => {
  count += 1;
  return R.sum(xs);
});

console.log(sumList([1, 2, 3]));
console.log(sumList([1, 2, 3]));
console.log(sumList([1, 2, 4]));
console.log(count);
Try It Yourself

How It Works

  1. Structured key. R.toString([1, 2, 3]) is a stable dump (not object identity).
  2. First list. R.sum → 6; stored under that string.
  3. Equal list. New array, same dump → cache hit; count stays 1.
  4. Different list. [1, 2, 4] → 7; count becomes 2.

Browser & Runtime Support

R.memoizeWith() is ordinary JavaScript once Ramda is loaded. It needs no special browser API — only a JS runtime that can run Ramda v0.24.0+.

✓ Ramda 0.24.0+ · Any JS runtime

Ramda.js R.memoizeWith()

Works in every modern browser and runtime where you can load Ramda 0.24.0 or later (CDN or npm). Older Ramda builds before 0.24.0 do not include memoizeWith; the old R.memoize helper was removed.

100% With Ramda 0.24+
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
R.memoizeWith() Universal

Bottom line: Safe in any project that includes Ramda 0.24.0 or later. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Bun, and Deno.

Frequently Asked Questions

R.memoizeWith(keyGen, fn) returns a new function that caches fn's results. On each call, keyGen runs on the same arguments and that string becomes the cache key. If the key is already in the cache, fn is skipped. Added in v0.24.0.
withAge uses o => `${o.birth}/${o.death}` as keyGen and returns { birth, death, age: death - birth }. The first withAge({birth: 1921, death: 1999}) computes age 78. The second call with the same dates returns the cached object and does not log 'computing age'.
R.memoize was removed. It tried to stringify every argument for you. memoizeWith makes you pick the key, so you can cache on the fields that matter and avoid accidental collisions.
Yes for a single primitive (numbers, strings). No for objects or multiple arguments: identity keeps only the first argument, and an object used as an object-key becomes '[object Object]', so every object can collide.
No. The cache is a closed-over object. It grows for every new key until the memoized function is discarded. Do not memoize unbounded user input without a bound.
No. R.memoizeWith is a stable Ramda function helper from v0.24.0. It is not deprecated, experimental, or non-standard. It replaced the removed R.memoize.

Next: R.nAry()

Wrap a function so it accepts exactly n arguments and ignores the rest.

R.nAry() 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.

12 people found this page helpful