Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
async for vs promise.all vs for await
(version: 0)
Comparing performance of:
async For vs PromiseAll vs For await
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
async function asyncFor(){ for (const x of [1, 2, 3]) { await new Promise(resolve => resolve(x > 0)); } } async function promiseAll() { await Promise.all([1, 2, 3].map(x => new Promise(resolve => resolve(x > 0)) )); } async function forAwait(){ for await (const x of [1, 2, 3]) { await new Promise(resolve => resolve(x > 0)); } }
Tests:
async For
asyncFor()
PromiseAll
promiseAll()
For await
forAwait()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
async For
PromiseAll
For await
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
async For
4187243.2 Ops/sec
PromiseAll
1460868.0 Ops/sec
For await
2881119.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare three different approaches for handling asynchronous iteration: 1. **async For**: uses `for...of` loop with `await` keyword inside the loop body. 2. **PromiseAll**: uses `Promise.all()` method to execute multiple promises concurrently. 3. **For Await**: uses `for await...of` loop, which is similar to traditional `for...of`, but allows using `await` expression inside the loop body. **Options Comparison** The three options differ in their approach to handling asynchronous iteration: * **async For**: executes each iteration synchronously, waiting for the promise resolution before moving on to the next iteration. * **PromiseAll**: executes all promises concurrently and collects the results when all promises are resolved. * **For Await**: executes each iteration asynchronously, allowing other tasks to run while waiting for the promise resolution. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **async For**: + Pros: simple and easy to understand, can be more efficient for small iterations. + Cons: may lead to performance issues if many promises are created in a single iteration, can block other tasks while waiting for promise resolution. * **PromiseAll**: + Pros: allows concurrent execution of multiple promises, can improve overall performance. + Cons: requires careful handling of error cases, may not be suitable for small iterations where individual promise resolution is important. * **For Await**: + Pros: similar to async For, but with the added benefit of being able to use `await` expression inside the loop body, allowing for more efficient execution. + Cons: requires support for `for await...of` syntax in the JavaScript engine. **Library and Special JS Features** The benchmark uses built-in JavaScript features: * **async/await**: used to handle asynchronous iteration in each approach. * **Promise.all()**: used to execute multiple promises concurrently in the PromiseAll approach. * **for await...of**: used in the For Await approach to iterate over values asynchronously. **Test Users Special JS Feature or Syntax** The benchmark uses the `for await` syntax, which was introduced in ECMAScript 2017 (ES7). This feature allows using `await` expression inside a loop to handle asynchronous iteration. **Other Alternatives** If you're interested in exploring other alternatives, here are a few options: * **Using Web Workers**: you can use web workers to execute tasks concurrently, which might be suitable for CPU-bound tasks. * **Using async/await with individual promises**: instead of using `Promise.all()`, you can use `async/await` to handle each promise individually, allowing more control over the execution order. * **Using other iteration methods**: there are other iteration methods available in JavaScript, such as using `forEach()` or `map()`, which might be suitable for specific use cases.
Related benchmarks:
Array loop vs foreach vs map vs for of
for vs foreach vs map 2
Array.apply vs .from vs map vs loop
Performance of JavaScript .forEach, .map and .reduce vs for and for..of (fork)
Array loop vs foreach vs map forsk
Comments
Confirm delete:
Do you really want to delete benchmark?