← All issues

Promise combinator fast path for non-thenable elements

4ed24b8

The ECMAScript spec mandates that Promise.all/allSettled/any/race call Promise.resolve(element) for each input, which checks for a callable .then property. For non-thenables this produces a fulfilled Promise whose sole purpose is to queue the downstream callback as a microtask — pure allocation overhead. The optimisation is only safe if the thenability check is performed without observable side effects.

This commit adds a fast path that skips allocating an intermediate Promise.resolve(value) JSPromise cell for provably non-thenable elements, instead queuing the resolver microtask directly. A new isNonThenable predicate gates the optimisation.

Benchmarks show 1.4–1.7x speedup for arrays of primitives or plain objects, but the isNonThenable predicate must be airtight: any false positive changes observable ECMAScript behaviour.

🔒

The soundness of the new thenability predicate across object model edge cases is worth close inspection — audit directions included.

Subscribe to read more