const array1 = [1,2,3,4,5,6,7,8,9,10];
const array = [];
for (let i = 0; i < 100; i++) {
array1.push(array);
}
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
console.log("helo");
(async () => {
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
});
console.log("helo");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Traditional for-loop (control group) | |
Async for-loop (experiment) |
Test name | Executions per second |
---|---|
Traditional for-loop (control group) | 0.0 Ops/sec |
Async for-loop (experiment) | 620258.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The benchmark is testing two approaches to iterate over an array: a traditional synchronous for-loop and an asynchronous for-loop using async/await syntax.
Options compared
The two options are:
for
loop to iterate over the array.async/await
syntax to iterate over the array.Pros and Cons of each approach
Library usage
In the provided benchmark code, there is no explicit library usage. However, it's worth noting that the async
and await
keywords are part of the JavaScript language itself, which has become more prominent in recent years due to the rise of asynchronous programming.
Special JS features or syntax
This benchmark uses a feature commonly known as "async/await" or "Promise-based I/O". Async/await is a way to write asynchronous code that looks and feels like synchronous code. It's a powerful tool for writing efficient, non-blocking code in JavaScript.
The test case also uses console.log
statements for logging output, which is the most common way to print output in JavaScript.
Other alternatives
In addition to these two approaches, there are other ways to iterate over arrays in JavaScript:
It's worth noting that the choice of iteration method ultimately depends on the specific use case, personal preference, and performance requirements.