demo · v143
Map.getOrInsert(Computed)
Try the new Map.prototype.getOrInsert and getOrInsertComputed in your browser. The factory only runs when the key is missing; the call always returns the current value. Same shape for WeakMap.
probes
try it
current map state
call log
the call
const m = new Map();
m.getOrInsert("alice", "A123");
// → "A123" (inserted)
m.getOrInsert("alice", "Z999");
// → "A123" (already present)
m.getOrInsertComputed("bob", (k) => expensive(k));
// factory only runs when bob is missing
see also
- Upsert — feature index
- ChromeStatus entry
- TC39 proposal