Node 26 shipped V8 14.6, and with it two new methods that have been asked for for years: Map.prototype.getOrInsert() and Map.prototype.getOrInsertC...
Some comments have been hidden by the post's author - find out more
Your warm-cache gap works out to about 2.4 ns a call (11.9 ms over 5M). When I benchmarked await in Node, a million calls to an empty sync function went from 2 ms to 51 ms with await in front, roughly 49 ns each. Different machines, but in your dedup example get() is async, so the await around the lookup costs something like 20x what the native method loses. Did you time the async get() path too?
no, I only timed the sync paths. but I think your math points the same way the post ends up: in the dedup case the 2.4 ns is noise either way. the await costs more than the lookup, and the fetch it's guarding costs milliseconds, so nobody picks this method for speed on that path, you pick it because three callers make one request.
one small thing in my own example though, get() doesn't need to be async at all. it just returns the cached promise, so dropping the async keyword skips the extra wrapper promise and the caller's await is the only one left. I'll change that in the snippet.
Dropping the async keyword makes sense. Then the caller's await is the only one left, and in my runs one await cost about 50 ns, nothing next to a fetch measured in milliseconds. Your post also says a rejected promise stays cached for good. Will the snippet delete the key on catch?
yes, it'll go in the same snippet, with an identity check so a late failure can't delete a newer promise:
function get(key) {
const p = cache.getOrInsertComputed(key, fetchData);
p.catch(() => { if (cache.get(key) === p) cache.delete(key); });
return p;
}
the caller still gets the rejection from p, the key is just gone so the next call fetches again. the plain .catch(() => cache.delete(key)) in the post's closing section is missing that check, so I'll fix it there too.
Interesting findings! The async promise caching part is especially useful because it prevents duplicate requests when multiple calls happen at the same time. But the rejected promise staying cached is something developers should be careful about. A good reminder that cleaner APIs don't always mean better performance. Great breakdown!
thanks. for the rejected promise, the simplest fix is to evict it when it fails, something like:
const p = cache.getOrInsertComputed(key, () => fetchThing(key));
p.catch(() => { if (cache.get(key) === p) cache.delete(key); });
the identity check matters, otherwise a slow failure can delete a newer promise that a later call already put there.
Great point! The identity check is an important detail, especially when handling concurrent requests. Evicting failed promises makes the cache more reliable and allows future retries. Thanks for sharing!
glad it helped, thanks for reading.
myGetOrInsertComputed function does not handle case when .get value is pulled from map and is actually undefined value, so I assume this causes difference as I think internally it must check first this with has(), before running rest of logic.
good catch, and you're right on both counts. "exactly what the native method does" was wrong: if a key holds undefined, the native method returns undefined without calling the callback, while my version calls it and overwrites. so mine was the get-then-set shortcut, not a faithful copy.
I wasn't sure it explained the speed gap, so I ran it again on Node 26.13 with a third JS version that checks has() first. medians, three runs:
warm, 5M calls over 1,000 keys: get-then-set ~65ms, has() then get ~98ms, native ~94ms
cold, 5M new keys: get-then-set ~880-930ms, has() version ~910-945ms, native ~945-957ms
so the native method costs about the same as the JS version that does it properly. most of the warm gap is that correctness check, not V8 neglecting a new builtin. I'll fix the post, thanks.
375358
تجربه دمج
امتثال