2026-11-11 · 8 min read

The Method You Asked For Is 40% Slower Than the Loop It Replaces

Flat isometric dark diagram of a key-value cache: indigo and violet cuboid slots connected by thin glowing indigo lines to a central node, with one green resolved entry and one grey failed entry.

For years, JavaScript developers asked the platform for one small favour: a way to say "get this key, and if it isn't there, compute it, store it, and hand it back" without retyping the same five lines. In Node 26, they got it. Then somebody benchmarked it, and the built-in lost to the five lines it was meant to replace — by roughly 40% on the workload real caches actually see.

That's the surprising part. It is not the useful part. The same method quietly changes what happens when your computation fails, and that change is the one that will page you at 2am.

What actually shipped

Node 26.13.0 bundles V8 14.6, which adds two methods to Map and WeakMap: getOrInsert(key, value) and getOrInsertComputed(key, callback). If the key is already present, you get the existing value and the callback never runs. If it isn't, the callback runs with the key, its return value is stored, and you get that back.

It replaces this shape, which you have written into a cache, a memo table, or a lookup helper at least once:

Look the key up. If what comes back is undefined, compute the value, store it, return it. Otherwise return what you found.

A boring, welcome convenience. Which is why nobody expected two surprises: it is measurably slower than the code it replaces, and it stores failures permanently.

The measurement: fine when cold, bad when warm

A developer benchmarked both patterns on Node 26.13.0 (V8 14.6.202.34), timing with high-resolution counters and reporting both the fastest and the median run across five to nine repetitions. Two workloads, chosen well:

  • Cold: 5,000,000 unique keys, every one a miss. The worst case, where the callback fires every single time.
  • Warm: 5,000,000 calls over the same 1,000 keys. The ordinary case, where a cache is supposed to be fast.

On the cold map, get-then-set took 1,678ms against 1,978ms for the built-in: about 18% slower. On the warm map, 29.7ms against 41.6ms: about 40% slower.

The most telling part came next. The author wrote a plain JavaScript function that does exactly what the native method does — same signature, same callback, called indirectly rather than inlined into the loop — and swapped the three implementations in alternating order across nine repetitions to cancel out CPU frequency drift and cache warm-up. The hand-rolled JavaScript version was never the slowest, and its best run (1,452.6ms) beat the native method's best run (1,890.8ms) on the cold workload. Your own function, with an extra frame and an extra branch, outperformed the engine's built-in.

One build, one machine, and no way to inspect V8's internals from the outside — so treat the exact percentages as a snapshot, not a law. But the ordering held across six runs and three independent implementations, which is more than you can say for most performance folklore.

Why a built-in can lose to your own loop

Engines get fast by specialisation. When V8 sees the same function called with the same shapes of data over and over, it stops asking questions and starts assuming — the technical term is an inline cache, and the practical effect is that hot, predictable calls get compiled down to almost nothing.

Map.prototype.get and Map.prototype.set have been hot for a decade. Generations of engine work assume you call them in a tight loop with a string or object key, and the fast paths are paved. A method that shipped weeks ago has not been through that. Every call still crosses out of your optimised code into the engine's implementation and back, and the compiler has not yet learned to make that crossing free.

Which produces a general rule that has nothing to do with JavaScript: a new standard method is not a faster standard method. It is a more portable one. If a method just entered the language, the honest expectation is that it is roughly as fast as the code you would have written, sometimes slower, and that the gap closes over the next two engine releases. Adopt it for correctness and readability. Never adopt it for speed without measuring.

Where it genuinely earns its place: the stampede

Here is the case where you should use it today, performance be damned.

A cache stampede happens when a popular key expires and a hundred callers ask for it at the same moment. Each one misses, each one starts its own expensive fetch, and you pay a hundred times for one answer. The classic get-then-set idiom does not protect you, because there is an await between the check and the write: every caller sees an empty map and starts working.

The new method inserts synchronously, before the promise settles. So if you memoise the promise rather than the resolved value, concurrent callers for the same key all receive the same in-flight promise. In the published test, three concurrent calls for one key plus one call for another produced exactly two underlying fetches instead of four.

That is the "in-flight promise map plus a finally() cleanup" pattern that a lot of server code hand-rolls, now available as one correct line instead of forty lines of code that three reviewers squinted at. For an API client, a per-request cache, or an agent that fans out tool calls, that is a real reduction in load and a real reduction in code you have to maintain.

The trap: a rejection is a value now

The same property that makes deduplication work is the one that bites you. The engine commits the promise to the map the instant your callback returns it — it does not wait to see whether that promise resolves or rejects. A rejected promise therefore sits in the cache exactly as a resolved one would, and nothing evicts it.

In the source's demonstration, the first call fails with a transient error. The second call returns the identical error without ever retrying. The third does too. Total attempts actually made against the flaky upstream: one. The function would have succeeded on its second attempt.

Translate that to production. A 30-second network blip, a rate limit, a pod restart upstream, one DNS hiccup — and that key is poisoned for the lifetime of the process. Every retry your users make hits the same cached rejection. Nothing in the method's name, its signature, or your code review hints that this is a failure cache. You will spend an afternoon blaming the upstream.

The fix is a wrapper, not a different method

Call the method with a callback that wraps your work: run the fetch, attach a catch that deletes the key and rethrows. Success gets cached, failure does not, and the next caller starts a fresh attempt. The cost is one extra function allocation per miss — nothing next to the network call you just saved.

If the upstream is genuinely down and you don't want a thousand callers hammering it, don't rely on an accidental failure cache. Add an explicit negative cache with a short expiry, or a bounded retry with backoff. The difference matters: one is a policy you chose and can tune, the other is a side effect you discovered in an incident review.

A decision framework

  1. Are you memoising a promise that several callers can request before it settles? Use getOrInsertComputed, wrapped so rejections are evicted. This is the case it was built for.
  2. Is it a hot synchronous memo of plain values, hit millions of times? Use get and set. It is measurably faster on today's build, and the difference is not noise.
  3. Can your computed value legitimately be undefined? Then the old idiom was already broken — it cannot tell "absent" from "present but undefined" and will recompute forever. The new method tests presence, so this is a correctness fix, not a convenience.
  4. Can the computation throw or reject? Then you need eviction, full stop. Skip that question and you have built a cache that remembers bad luck.

What to take back to your team

Two habits, both cheap. First, benchmark in your loop shape, not a blog post's loop shape. A number measured on five million cold writes tells you very little about a warm request cache serving a thousand keys — and vice versa. The published test only became useful because it ran both.

Second, treat a new standard method like a new dependency: read what it does when things go wrong, not just what it does when they go right. Changelogs announce availability, not behaviour. The reason getOrInsertComputed is worth understanding is not that it is 40% slower; it is that a convenience method silently redefined what happens after a failure, and no release note would have told you that.

Key Takeaways

  • A built-in is not automatically faster. Node 26's getOrInsertComputed measured about 18% slower on an all-miss workload and about 40% slower on a warm cache than the five-line get-then-set idiom it replaces.
  • Your own function can beat the engine's. A hand-written JavaScript equivalent was never slower than the native method across alternating runs — new built-ins haven't had years of compiler specialisation behind them yet.
  • Its real win is async deduplication. Because it inserts synchronously, concurrent callers share one in-flight promise: four calls for two keys became two fetches, not four.
  • Failures are cached forever. A rejected promise is committed to the map like any other value. One transient error poisons that key until the process restarts.
  • Wrap it or don't use it for async work. Catch, delete the key, rethrow. Then choose your retry or negative-cache policy deliberately instead of discovering it during an incident.

[ Call to action ]

Ready to scale your engineering team?

Tell us the roles you need to fill and we'll get back within 24 hours.