var array = Array.from({
length: 100
}, () => String.fromCharCode((Math.random() * 26) + 65 | 0)).join('');
for (var i = 0; i < array.length; i++) {
array[i];
}
for (var i of array) {
array[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
for..of |
Test name | Executions per second |
---|---|
for | 42519.4 Ops/sec |
for..of | 31190.6 Ops/sec |
Let's break down the benchmark and explain what's being tested, compared, and their pros and cons.
What is being tested?
The benchmark measures the performance difference between two loop types: traditional for
loops and for...of
loops with arrays. The test case specifically compares these two approaches when iterating over a large array of random strings.
Options compared:
for
loops: This approach uses an explicit counter variable (i
) to iterate over the array indices.for...of
loops: This approach uses the iterator protocol to iterate over the array elements, without needing an explicit counter variable.Pros and Cons of each approach:
for
loops:i
) and array indices.for...of
loops:Library usage:
In this benchmark, the Array.from
method is used to create a large array of random strings. This library function creates an array from an iterable (in this case, a function that generates random characters) and returns it as an array. The purpose of using Array.from
is to ensure consistent performance characteristics for both loop types.
Special JS feature or syntax:
None mentioned in the provided benchmark code.
Other considerations:
ExecutionsPerSecond
metric represents the average number of executions per second, which is an important performance indicator for this benchmark.Alternative approaches:
Other alternatives could include:
while
loops or recursive functions.forEach
, map
, or filter
).Keep in mind that these alternatives might not be relevant to this specific benchmark, and the comparison between traditional for
loops and for...of
loops is likely the primary focus of MeasureThat.net's analysis.