((cb) => {cb()})(() => {});
(()=>Promise.resolve())();
(async() => { await true; })();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
callbacks | |
promises | |
async/awaits |
Test name | Executions per second |
---|---|
callbacks | 176784224.0 Ops/sec |
promises | 76002240.0 Ops/sec |
async/awaits | 6909932.5 Ops/sec |
Let's break down what's being tested in this benchmark.
What's being compared:
The benchmark is comparing three different approaches to handling asynchronous operations:
((cb) => {cb()})(() => {})
defines a callback function that immediately calls itself.async
and await
keywords to wait for the completion of an asynchronous operation.Pros and Cons:
Library:
None explicitly mentioned in the benchmark definition, but Promise
is a built-in JavaScript object used in the promise approach.
Special JS feature/syntax:
The async/await syntax requires support for modern browsers (including Firefox 129) or Node.js versions. The promises and callbacks approaches are more widely supported, but async/await is often considered more readable and concise.
Other alternatives:
then
method: Instead of using a promise, you can use a callback function inside another function.Keep in mind that this benchmark focuses on comparing three specific approaches and might not cover all possible alternatives or edge cases.