WeakMap.prototype.getOrInsertComputed()

Baseline 2026
Newly available

Since February 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The getOrInsertComputed() method of WeakMap instances returns the value corresponding to the specified key in this WeakMap. If the key is not present, it inserts a new entry with the key and a default value computed from a given callback, and returns the inserted value.

Use this method instead of WeakMap.prototype.getOrInsert() when the default value is expensive to compute, and you want to avoid computing it unless it's actually needed.

Try it

const alan = { name: "Alan" };
const map = new WeakMap([[alan, 35]]);
const defaultCreator = (obj) => `${obj.name}'s age is unknown`;

console.log(map.getOrInsertComputed(alan, defaultCreator));
// Expected output: 35

const brett = { name: "Brett" };
console.log(map.getOrInsertComputed(brett, defaultCreator));
// Expected output: "Brett's age is unknown"

Syntax

js
getOrInsertComputed(key, callback)

Parameters

key

The key of the element to return from the Map object. Must be either an object or a non-registered symbol. Object keys are compared by reference, not by value.

callback

A function that returns the value to insert and return if the key is not already present in the Map object. The function is called with the following argument:

key

The same key that was passed to getOrInsertComputed().

Return value

The value associated with the specified key in the WeakMap object. If the key can't be found, the result of callback(key) is inserted and returned.

Exceptions

TypeError

Thrown in one of the following cases:

Examples

Caching

Caching involves building a mapping from the parameter of an expensive function to its return value, so that future calls with the same parameter can return the cached value instead of recomputing it. When no cached value exists, it needs to be computed and inserted into the cache.

We use a WeakMap here instead of a Map, so that the cached argument values do not prevent them from being garbage-collected if they are no longer needed elsewhere in the program. If your cached function takes non-object arguments, you can use a Map instead.

js
// Any expensive function you want to cache
function computeExpensiveValue(requestOptions) {
  // Imagine expensive computation, like fetching data or complex calculations
  console.log(`Fetching from ${requestOptions.url}`);
  return new Response("Fake response");
}

// A higher-order function that adds caching to any function
function withCache(fn) {
  const cache = new WeakMap();
  return (param) => cache.getOrInsertComputed(param, fn);
}

const computeWithCache = withCache(computeExpensiveValue);
const options = { url: "https://example.com/a", method: "GET" };
const value1 = computeWithCache(options); // Logs "Fetching from https://example.com/a"
const value2 = computeWithCache(options); // No log

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-weakmap.prototype.getorinsertcomputed

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
getOrInsertComputed
Chrome – Full support
Chrome 145 (Release date: 2026-02-10)
footnote Full support
Edge – Full support
Edge 145 (Release date: 2026-02-14)
footnote Full support
Firefox – Full support
Firefox 144 (Release date: 2025-10-14)
footnote Full support
Opera – Full support
Opera 129 (Release date: 2026-03-18)
footnote Full support
Safari – Full support
Safari 26.2 (Release date: 2025-12-12)
footnote Full support
Chrome Android – Full support
Chrome Android 145 (Release date: 2026-02-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 144 (Release date: 2025-10-14)
footnote Full support
Opera Android – Full support
Opera Android 96 (Release date: 2026-03-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 145 (Release date: 2026-02-10)
footnote Full support
WebView on iOS – Full support
WebView on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Bun – Full support
Bun 1.2.20 (Release date: 2025-08-10)
footnote Full support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also