WeakMap.prototype.getOrInsertComputed()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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.

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
Upsert
# sec-weakmap.prototype.getOrInsertComputed

Browser compatibility

See also