var smallArray = [];
var mediumArray = [];
var largeArray = [];
var hugeArray = [];
for (let i = 0; i < 10; i++) {
smallArray.push(i);
}
for (let i = 0; i < 100; i++) {
mediumArray.push(i);
}
for (let i = 0; i < 1000; i++) {
largeArray.push(i);
}
for (let i = 0; i < 10000; i++) {
hugeArray.push(i);
}
smallArray.forEach(item => {
item += 1;
})
for (let i = 0, l = smallArray.length; i < l; i++) {
smallArray[i] += 1;
}
mediumArray.forEach(item => {
item += 1;
})
for (let i = 0, l = mediumArray.length; i < l; i++) {
mediumArray[i] += 1;
}
largeArray.forEach(item => {
item += 1;
})
for (let i = 0, l = largeArray.length; i < l; i++) {
largeArray[i] += 1;
}
hugeArray.forEach(item => {
item += 1;
})
for (let i = 0, l = hugeArray.length; i < l; i++) {
hugeArray[i] += 1;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
smallArray forEach | |
smallArray for loop | |
mediumArray forEach | |
mediumArray for loop | |
largeArray forEach | |
largeArray for loop | |
hugeArray forEach | |
hugeArray for loop |
Test name | Executions per second |
---|---|
smallArray forEach | 16400891.0 Ops/sec |
smallArray for loop | 1666786.1 Ops/sec |
mediumArray forEach | 10257451.0 Ops/sec |
mediumArray for loop | 185359.2 Ops/sec |
largeArray forEach | 2795250.2 Ops/sec |
largeArray for loop | 18683.2 Ops/sec |
hugeArray forEach | 48655.7 Ops/sec |
hugeArray for loop | 1892.9 Ops/sec |
Let's dive into the benchmark.
The provided JSON represents a JavaScript microbenchmarking test case. The main goal of this benchmark is to compare the performance of using forEach
loops versus traditional for
loops in JavaScript for element iteration and modification.
Benchmark Definition
The benchmark definition is an array that contains multiple individual tests, each with its own script preparation code and HTML preparation code (which is empty in this case). The script preparation code generates four arrays with different sizes: small, medium, large, and huge. Each test uses either the forEach
loop or a traditional for
loop to increment the elements of an array.
Options Compared
Two main options are compared:
forEach
Loop: This is a method that executes a provided function once for each element in an array.for
Loop: A classic way to iterate over an array, using the index variable i
and the length property of the array.Pros and Cons
forEach
Loop:for
Loop:Library Used
The only library used in this benchmark is the built-in JavaScript Array.prototype.forEach
method. This method provides a convenient way to iterate over arrays without manually handling indices.
Performance Results
The performance results show that the forEach
loop performs slightly worse than the traditional for
loop for large array sizes, while performing similarly or even better for smaller arrays. However, these differences may be negligible in many real-world scenarios, and the choice between forEach
and traditional loops ultimately depends on code readability, maintainability, and personal preference.
Keep in mind that this benchmark is specific to JavaScript and might not be directly applicable to other programming languages or use cases.