async function asyncFor(){
for (const x of [1, 2, 3]) {
await new Promise(resolve => setTimeout(resolve(x > 0), 100));
}
}
async function asyncPromiseAll() {
await Promise.all([1, 2, 3].map(x =>
new Promise(resolve => setTimeout(resolve(x > 0), 100))
));
}
asyncFor()
asyncPromiseAll()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
asyncFor | |
asyncPromiseAll |
Test name | Executions per second |
---|---|
asyncFor | 361789.4 Ops/sec |
asyncPromiseAll | 125483.1 Ops/sec |
I'll break down the provided benchmark definition, options being compared, pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark measures the performance difference between two approaches: async for
and Promise.all2
. Both methods are used to iterate over an array of promises with a timeout.
Script Preparation Code
The script preparation code defines two functions:
asyncFor()
: Iterates over the array [1, 2, 3]
using a traditional for...of
loop. Inside the loop, it waits for 100 milliseconds using new Promise(resolve => setTimeout(resolve(x > 0), 100))
. This creates a promise chain where each iteration waits for the previous one to complete.asyncPromiseAll()
: Uses Promise.all()
with an array map to create promises in parallel. It maps over the array [1, 2, 3]
, creating a promise for each element using new Promise(resolve => setTimeout(resolve(x > 0), 100))
. The await
keyword is used to wait for all promises to resolve.Html Preparation Code
The html preparation code is empty, which means it's not relevant to the benchmarking process.
Individual Test Cases
There are two test cases:
asyncFor()
: Tests the performance of the async for
approach.asyncPromiseAll()
: Tests the performance of the Promise.all2
approach.Options Being Compared
The benchmark compares the performance of two approaches:
for...of
loop with promises: asyncFor()
Promise.all()
with array map: asyncPromiseAll()
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
async for
:Promise.all()
because it creates a promise chain that waits for each iteration to complete.Promise.all2
:Library Used
None, as both approaches are built-in JavaScript features.
Special JS Feature/ Syntax
The benchmark uses a feature called "await" which was introduced in ECMAScript 2017. The async
keyword is used to define an asynchronous function, and await
is used to pause the execution of the function until the promise is resolved or rejected.
Other Considerations
When choosing between these approaches, consider the following:
async for
when:Promise.all2
when:Other Alternatives
If you need to compare other approaches, consider using:
for...of
loop without promises: This approach is similar to async for
, but without the use of promises.Promise.all()
with custom map function: Instead of using an array map, you can create a custom function that maps over the array and creates promises in parallel.Keep in mind that the choice of approach depends on the specifics of your project, including performance requirements, code readability, and maintainability.