JavaScript programmers may expect that the following
two programs are largely similar in terms of how they perform with
respect to the ECMAScript job queue (if inside of an async function):
promise.then(f); f(await promise);
However, if `promise` is a built-in promise, then these two code fragments
will differ in the number of iterations through the job queue are taken: because
`await` always wraps a Promise with another Promise, there are three job
queue items enqueued and dequeued before calling `f` in the `await` example,
whereas there is just a single item for the `then` usage.
In discussions with JavaScript programmers, the number of job queue items
in the current semantics turns out to be surprising. For example, the difference
has become more visible in conjunction with new V8 features for Promise
visibility and debugging, which are sometimes used in Node.js.
This patch changes the semantics of await to reduce the number of
job queue turns that are taken in the common `await` Promise case by replacing
the unconditional wrapping with a call to PromiseResolve. Analogous changes
are made in async iterators.
The patch preserves key design goals of async/await:
- Job queue processing remains deterministic, including both ordering and the number of jobs enqueued (which is observable by interspersing other jobs)
- Userland Promise libraries with "then" methods ("foreign thenables") are usable within `await`, and trigger a turn of the job queue
- Non-Promises can be awaited, and this takes a turn of the native job queue (as do all usages of `await`)
Reducing the number of job queue turns also improves performance
on multiple highly optimized async/await implementations. In a draft
implementation of this proposal in V8 behind a flag [1]:
- The doxbee async/await performance benchmark [2] improved with 48%
- The fibonacci async/await performance benchmark [3] improved with 23%
- The Hapi throughput benchmark [4] improved with 50% (when async hooks are enabled) and with 20% (when async hooks are disabled)
[1] https://chromium-review.googlesource.com/c/v8/v8/+/1106977
[2] https://github.com/bmeurer/promise-performance-tests/blob/master/lib/doxbee-async.js
[3] https://github.com/bmeurer/promise-performance-tests/blob/master/lib/fibonacci-async.js
[4] https://github.com/fastify/benchmarks