Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
async for vs promise all (setTimeout version)
(version: 0)
Inspired by: https://www.measurethat.net/Benchmarks/Show/13019/0/async-for-vs-promiseall
Comparing performance of:
asyncFor vs asyncPromiseAll
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function emptyTimeout(ms) { return new Promise((resolve, reject) => setTimeout(() => resolve(), ms)); } async function asyncFor() { for (let i = 50; i < 500; i += 50) { await emptyTimeout(i); } } async function asyncPromiseAll() { const promises = []; for (let i = 50; i < 500; i += 50) { promises.push(emptyTimeout(i)); } await Promise.all(promises); }
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
19950.9 Ops/sec
asyncPromiseAll
732.8 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 measures the performance of two approaches to iterate over an array asynchronously: 1. `asyncFor()`: This approach uses an asynchronous for loop, which iterates over the array using a traditional for loop but yields control to other tasks (like `setTimeout`) at each iteration. 2. `asyncPromiseAll()`: This approach uses Promise.all, which executes an array of promises and waits for all of them to resolve before continuing. **Options Compared:** The two approaches are compared in terms of execution time. The benchmark aims to determine which approach is faster for this specific use case. **Pros and Cons:** 1. `asyncFor()`: * Pros: Can be more efficient when dealing with large arrays because it doesn't create multiple promises, reducing memory usage. * Cons: May not be as readable or maintainable due to the use of async/await syntax and traditional for loop. 2. `asyncPromiseAll()`: * Pros: More concise and readable code. It's easier to understand what the function does at a glance. * Cons: Creates multiple promises, which can lead to higher memory usage and slower execution times due to the overhead of creating and managing promises. **Library Used:** None explicitly mentioned in the benchmark definition, but it uses Promises, which are built into JavaScript. Promises are used as a way to handle asynchronous operations in a more manageable and readable way. **Special JS Feature or Syntax:** The benchmark uses async/await syntax, which is a modern feature introduced in ECMAScript 2017 (ES7). Async/await allows developers to write asynchronous code that looks like synchronous code, making it easier to read and maintain. **Other Considerations:** When writing this benchmark, the author likely considered factors such as: * Execution time: The benchmark focuses on measuring how long each approach takes to complete. * Memory usage: The use of promises in `asyncPromiseAll()` may lead to higher memory usage due to the creation of multiple promises. * Readability and maintainability: The choice of approach affects how easy it is for developers to understand and modify the code. **Alternatives:** Other approaches to iterate over an array asynchronously might include: 1. `forEach()`: This method executes a callback function once for each element in an array, without creating promises. 2. Generators: A generator function can be used to create an iterator that yields values one at a time, which can be used with a loop or other async/await approaches. 3. Iterators: JavaScript's built-in iterators allow developers to create custom iteration mechanisms, potentially offering better performance than the approaches tested in this benchmark. Keep in mind that these alternatives might have different trade-offs and may not offer significant improvements over the tested approaches.
Related benchmarks:
async for vs promise.all2
async for vs promise.all /w delay
async for vs promise.all vs promise.allSettled
async for vs promise.all large 2
Comments
Confirm delete:
Do you really want to delete benchmark?