Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Promise vs Async Await
(version: 0)
Comparing performance of:
Promise Chain vs Async Await vs Promise.all
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function executePromises() { let 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); }); } async function executeAwaits() { const val = await Promise.resolve(1); const val2 = await Promise.resolve(2); const val3 = await Promise.resolve(3); const val4 = await Promise.resolve(4); const val5 = await Promise.resolve(5); const val6 = await Promise.resolve(6); const vals = [val, val2, val3, val4, val5, val6]; 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()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Promise Chain
Async Await
Promise.all
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Promise Chain
2058270.9 Ops/sec
Async Await
4223077.0 Ops/sec
Promise.all
893140.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark tests three approaches to handling promises in JavaScript: 1. **Promise Chain**: This approach uses a chain of `then()` methods to handle the promise resolution. 2. **Async/Await**: This approach uses the async/await syntax to handle the promise resolution. 3. **Promise.all**: This approach uses the `Promise.all()` method to handle multiple promises concurrently. **Options Comparison** Here's a brief comparison of the three approaches: ### Promise Chain * **Pros:** * Easy to understand and implement, especially for developers familiar with synchronous code. * Can be more efficient in certain cases, as it avoids the overhead of creating new promise instances. * **Cons:** * Can lead to callback hell (nested `then()` chains) if not managed carefully. * May result in slower performance due to the creation of multiple promise instances. ### Async/Await * **Pros:** * Readable and easier to write compared to traditional promise chaining. * Reduces the risk of callback hell, making code more maintainable. * Supports asynchronous iteration (async/await) and generator functions, which can improve performance in certain cases. * **Cons:** * May have a slight performance overhead due to the use of async functions and await expressions. ### Promise.all * **Pros:** * Efficiently handles multiple promises concurrently, reducing overall execution time. * Simplifies code when dealing with arrays of promises. * **Cons:** * Can lead to unexpected behavior if not used carefully (e.g., handling promise rejections). * May require additional error handling logic. **Library and Special Features** None of the test cases use a specific JavaScript library. However, they do utilize some special features: * **Async/Await**: The async/await syntax is used in the `executeAwaits()` function. * **Promise.all**: This method is used to handle multiple promises concurrently. **Other Considerations** When choosing an approach for handling promises, consider the following factors: * Readability and maintainability: Async/await might be a better choice if code readability is crucial. * Performance: Promise.all could be more efficient in some cases, especially when dealing with large numbers of promises. * Complexity: If you're working on a simple project or need to handle promises infrequently, Promise Chain might be sufficient. **Alternatives** Other approaches for handling promises include: * **Promise.then() and .catch()**: These methods are used in conjunction to manage promise resolutions and rejections. * **Generator functions and async generators**: These allow asynchronous iteration and can improve performance in certain cases. These alternatives offer varying trade-offs between readability, maintainability, and performance. The choice of approach depends on your specific use case and personal preference.
Related benchmarks:
Promise vs. Callback
Promise vs async vs callbacks
Promise vs Async Await(2)
callbacks vs promises vs async/awaits
Comments
Confirm delete:
Do you really want to delete benchmark?