Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
async for vs promise.all /w delay
(version: 0)
Comparing performance of:
asyncFor vs asyncPromiseAll
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 => setTimeout(() => resolve(x > 0), 500)); } } async function asyncPromiseAll() { await Promise.all([1, 2, 3].map(x => new Promise(resolve => setTimeout(() => resolve(x > 0), 500)) )); }
Tests:
asyncFor
asyncFor()
asyncPromiseAll
asyncPromiseAll()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
asyncFor
asyncPromiseAll
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Browser/OS:
Chrome 108 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
asyncFor
21210.6 Ops/sec
asyncPromiseAll
6555.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares two approaches for iterating over an array in JavaScript: `async/await` with a `for...of` loop (`asyncFor`) and using `Promise.all()` to wait for all promises to resolve (`asyncPromiseAll`). The test also includes a delay between iterations to simulate a slower execution. **Options Compared** The benchmark compares two main options: 1. **async/await with a for...of loop (asyncFor)**: This approach uses the `for...of` loop, which is designed for iterating over arrays, but requires manual handling of promises using `new Promise()`. The loop iterates over each element in the array, waiting for 500ms before resolving the promise. 2. **Promise.all() with array map (asyncPromiseAll)**: This approach uses `Promise.all()` to wait for all promises in an array to resolve. The array is mapped to create new promises, and then `Promise.all()` is used to wait for all promises to settle. **Pros and Cons of Each Approach** 1. **async/await with a for...of loop (asyncFor)**: * Pros: Easy to read and understand, allows for manual control over the iteration process. * Cons: Requires manual handling of promises, which can lead to errors if not implemented correctly. 2. **Promise.all() with array map (asyncPromiseAll)**: * Pros: Simplifies the iteration process by leveraging built-in promise handling, reduces error chances due to centralized promise management. * Cons: May have additional overhead due to creating multiple promises and using `Promise.all()`. **Library Used** The benchmark uses the `setTimeout()` function, which is a built-in JavaScript library. The `new Promise()` constructor also comes from the JavaScript standard library. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. However, note that using `await` outside of an `async` function is not supported in older browsers and environments without transpilation. **Other Alternatives** Alternative approaches for iterating over arrays include: 1. **for...in**: While generally discouraged due to potential issues with object iteration, it can be used for array iteration in some cases. 2. **Array.prototype.forEach()**: This method is designed specifically for iterating over arrays and is more concise than using `for` loops. 3. **Map()**: JavaScript 1.0 introduced the `map()` method, which creates a new array with the results of applying a provided function on every element in this array. Keep in mind that each approach has its own strengths and weaknesses, and the choice ultimately depends on the specific requirements and constraints of your project.
Related benchmarks:
For loop map vs map builtin for 100000 elements
for vs foreach vs map 2
Array fill method vs for loop vs map
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?