Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sync for vs promise.all vs async for
(version: 0)
Comparing performance of:
syncFor vs asyncPromiseAll vs asyncFor
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function syncFor() { let y = 0; for (const x of [1, 2, 3, 4, 5, 6, 7, 8, 9]) { y += x; } } async function asyncFor() { let y = 0; for (const x of [1, 2, 3, 4, 5, 6, 7, 8, 9]) { await new Promise((resolve) => { y += x; resolve(y); }); } } async function asyncPromiseAll() { let y = 0; await Promise.all([1, 2, 3, 4, 5, 6, 7, 8, 9].map(x => new Promise((resolve) => { y += x; resolve(y); }), )); }
Tests:
syncFor
syncFor()
asyncPromiseAll
asyncPromiseAll()
asyncFor
asyncFor()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
syncFor
asyncPromiseAll
asyncFor
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
syncFor
12798734.0 Ops/sec
asyncPromiseAll
288129.1 Ops/sec
asyncFor
1681272.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its results, explaining what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The test compares three different approaches for iterating over an array of numbers and summing them up: 1. **Sync Loop**: A traditional synchronous loop using `for...of` that adds the numbers to a variable `y`. 2. **Async Loop**: An asynchronous loop using `await` within a promise to add the numbers to `y`. 3. **Promise.all**: Using `Promise.all` to iterate over an array of promises that return the sum of each number added to `y`. **Test Case Comparison** The three test cases are: * `syncFor()`: Measures the execution time of the sync loop. * `asyncFor()`: Measures the execution time of the async loop. * `asyncPromiseAll()`: Measures the execution time of using `Promise.all` for iteration. **Comparison Options** 1. **Sync Loop**: This approach is straightforward but can be slow due to the synchronous nature of the loop, which blocks other tasks. * Pros: Easy to understand and implement. * Cons: Can lead to poor performance in concurrent systems. 2. **Async Loop**: Using `await` within a promise allows for non-blocking execution, making it more suitable for concurrent systems. * Pros: Can improve performance by allowing other tasks to run while waiting for the loop to finish. * Cons: Requires careful handling of promises and may introduce additional complexity. 3. **Promise.all**: This approach uses an array of promises to iterate over the numbers, which can be a powerful tool for concurrent execution. * Pros: Can handle large datasets efficiently and allows for parallel execution. * Cons: May require more complex setup and handling of promise resolution. **Library Used** In this benchmark, no specific library is used besides the built-in `Promise` API. However, using external libraries or frameworks can introduce additional complexity and performance overhead. **Special JS Features/Syntax** The benchmark uses the following special features: * **Await**: Used in the async loop to pause execution until a promise resolves. * **Async/await syntax**: Allows for more concise and readable asynchronous code. In summary, this benchmark helps determine which approach (sync loop, async loop, or Promise.all) is most efficient for iterating over an array of numbers and summing them up. The results can provide insights into the performance characteristics of each approach in a specific use case.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
Benchmark: flatMap vs reduce vs while vs foreach
Array fill method vs for loop_q
Comments
Confirm delete:
Do you really want to delete benchmark?