const items = Array.from({length: 10}, () => Math.floor(Math.random() * 40));
let index = 0;
items.forEach(i => index++);
const items = Array.from({length: 10}, () => Math.floor(Math.random() * 40));
let index = 0;
for (let i in items) {
index++;
}
const items = Array.from({length: 10}, () => Math.floor(Math.random() * 40));
let index = 0;
for (let i of items) {
index++;
}
const items = Array.from({length: 10}, () => Math.floor(Math.random() * 40));
let index = 0;
for(let i = 0; i < items.length; ++i) {
index++;
}
const items = Array.from({length: 10}, () => Math.floor(Math.random() * 40));
let index = 0;
for(let i = items.length; i > 0; --i) {
index++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for-in | |
for-of | |
for | |
optimized-for |
Test name | Executions per second |
---|---|
foreach | 157075.2 Ops/sec |
for-in | 88800.1 Ops/sec |
for-of | 160777.2 Ops/sec |
for | 198278.4 Ops/sec |
optimized-for | 212082.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark definition is provided in JSON format, which describes the test scenario. In this case, it's a simple loop that iterates over an array of numbers. The script preparation code is empty, indicating that no specific setup or initialization is required for the benchmark.
Here's what's being tested:
Array.from()
.index
is initialized to 0.foreach
, for-in
, for-of
, and a custom "optimized-for" loop.Loop Options
Let's examine each loop option:
foreach
: This loop uses the forEach()
method, which iterates over the array elements using a callback function.for-in
: This loop uses the in
operator to iterate over the array properties (i.e., indices).for-of
: This loop uses the for...of
statement, which iterates over the array elements using a simple iterator syntax.for
loop with indexing, which is often considered the most efficient way to iterate over arrays.Browser-Specific Considerations
The benchmark results are provided for a specific Firefox 48 browser on a Mac OS X 10.6 desktop platform. The results may not be representative of other browsers, platforms, or JavaScript engines.
Skip Preambles
As you mentioned, we'll skip the preamble and dive into the test cases.
Let's take a closer look at each test case:
foreach
for-in
for-of
for
Each of these tests iterates over the same array, but using different loop constructs. The results will help determine which loop construct is most efficient in this specific scenario.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few:
Array.prototype.map()
: Instead of using a loop to iterate over an array, you can use the map()
method to create a new array with transformed elements.Promise.all()
: If your test involves asynchronous operations or callbacks, you may want to consider using Promise.all()
to parallelize the execution.Keep in mind that these alternatives may not be suitable for every benchmarking scenario and may require more complex setup or modifications to your test case.
If you have any specific questions about the benchmark results or would like to explore alternative approaches, feel free to ask!