Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Promise vs Async Await(2)
(version: 0)
Comparing performance of:
Promise Chain vs Async Await vs Promise.all vs Promise Chain normal
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function executePromises() { const vals = []; Promise.resolve(1) .then((val) => { vals.push(val); return Promise.resolve(2); }) .then((val) => { vals.push(val); return Promise.resolve(3); }) .then((val) => { vals.push(val); return Promise.resolve(4); }) .then((val) => { vals.push(val); return Promise.resolve(5); }) .then((val) => { vals.push(val); return Promise.resolve(6); }) .then((val) => { vals.push(val); console.log(vals); }); } function executePromises2() { const vals = []; Promise.resolve(1) .then((val) => { vals.push(val); return 2; }) .then((val) => { vals.push(val); return 3; }) .then((val) => { vals.push(val); return 4; }) .then((val) => { vals.push(val); return 5; }) .then((val) => { vals.push(val); return 6; }) .then((val) => { vals.push(val); console.log(vals); }); } async function executeAwaits() { const vals = []; vals.push(await Promise.resolve(1)); vals.push(await Promise.resolve(2)); vals.push(await Promise.resolve(3)); vals.push(await Promise.resolve(4)); vals.push(await Promise.resolve(5)); vals.push(await Promise.resolve(6)); console.log(vals); } function executePromiseAll() { Promise.all([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4), Promise.resolve(5), Promise.resolve(6)]).then( (vals, rej) => { console.log(vals); } ); }
Tests:
Promise Chain
executePromises();
Async Await
executeAwaits();
Promise.all
executePromiseAll();
Promise Chain normal
executePromises2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Promise Chain
Async Await
Promise.all
Promise Chain normal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Promise Chain
204986.5 Ops/sec
Async Await
197922.0 Ops/sec
Promise.all
18656.2 Ops/sec
Promise Chain normal
29457.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided benchmark tests three different approaches to handling promises and asynchronous code in JavaScript: 1. **Promise Chain**: A traditional promise chain approach, where each `then` method returns a new promise, allowing for sequential execution. 2. **Async/Await**: An async/await approach, which simplifies the promise chain by using a more readable syntax. 3. **Promise.all**: A way to execute multiple promises concurrently and wait for all of them to resolve. **Options compared** In each test case: * The number of promises in the chain or array is different (2 vs 6). * One approach uses async/await, while others use traditional promise chaining. * Each approach has a unique execution pattern: Promise Chain executes sequentially, Async/Await executes sequentially with await, and Promise.all executes concurrently. **Pros and Cons** 1. **Promise Chain** * Pros: + Simple to implement + Easy to understand for beginners * Cons: + Can be slow due to sequential execution + Error handling can become cumbersome with nested `catch` blocks 2. **Async/Await** * Pros: + More readable and concise syntax + Easier to handle errors with try-catch blocks + Faster execution compared to Promise Chain (due to compilation to native code) * Cons: + May not be suitable for all use cases, especially those requiring complex promise chaining 3. **Promise.all** * Pros: + Executes multiple promises concurrently, potentially improving performance + Easy to handle errors with a single catch block * Cons: + Requires an array of promises, which might not be suitable for all scenarios + Can lead to unexpected behavior if not used correctly **Special considerations** In this benchmark, the `executeAwaits()` test case uses async/await, while others use traditional promise chaining. This highlights the trade-offs between readability, performance, and complexity in handling promises. **Other alternatives** If you'd like to explore other approaches, consider: * **Cooperative scheduling**: A different concurrency model that allows developers to explicitly control execution order. * **Micro-threads**: Lightweight threads that can be used to execute code concurrently without the overhead of full-fledged threads. * **JavaScript worklets**: A new API for executing micro-batches of JavaScript code in parallel. However, these alternatives are not directly comparable to the promise-based approaches tested here.
Related benchmarks:
Array.prototype.concat vs spread operator vs stringify
Array.prototype.map.call() vs [...array].map()
fill array with value: map(callback) vs fill(value)
Array deconstruction vs array push
flatMap vs Reduce with push - test2
Comments
Confirm delete:
Do you really want to delete benchmark?