var array = new Array(100).fill(1);
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
let sum = 0;
array.forEach(value => {
sum += value;
});
let sum = 0;
for (let i in array) {
sum += array[i];
}
let sum = 0;
for (let value of array) {
sum += value;
}
let sum = 0, len = array.length;
for (let i = 0; i < len; i++) {
sum += array[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
for in | |
for..of | |
cached for |
Test name | Executions per second |
---|---|
for | 4783004.0 Ops/sec |
foreach | 6698160.0 Ops/sec |
for in | 541491.6 Ops/sec |
for..of | 12056550.0 Ops/sec |
cached for | 6856605.5 Ops/sec |
The provided benchmark tests different looping constructs in JavaScript to determine their performance when summing values in an array. Each test case represents a unique approach to iterating over the elements of the array, capturing how computational efficiency varies between them. Here’s an overview of the individual test cases and a comparison of their pros and cons:
For Loop (for
)
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
ForEach Method (foreach
)
let sum = 0;
array.forEach(value => {
sum += value;
});
for
loop, as it involves a function call for each iteration, resulting in overhead.For In Loop (for in
)
let sum = 0;
for (let i in array) {
sum += array[i];
}
For Of Loop (for..of
)
let sum = 0;
for (let value of array) {
sum += value;
}
for
loop due to internal iterator mechanics.Cached For Loop (cached for
)
let sum = 0, len = array.length;
for (let i = 0; i < len; i++) {
sum += array[i];
}
array.length
is accessed. It can result in improved performance in scenarios where an array is very large.for
loop but introduces a caching mechanism that is not always necessary for smaller arrays.From the benchmark results, we observe the following executions per second:
for..of
with 12,462,154 executions per second, indicating its efficiency, possibly due to its optimized implementation for iterables.forEach
follows, at 7,937,888.5, which, while readable, does not match the speed of traditional loops.cached for
comes next at 7,471,507.5, showing the benefits of caching but still trailing behind the highest-performing constructs.for
is next at 5,380,119.5, which is a solid performer.for in
at 736,892.25, confirming its unsuitability for arrays due to its iteration over all enumerable properties.for..of
construct is a relatively new addition in ES6 that enhances the way we iterate over iterables. Its semantic clarity and ability to function with any iterable types make it a favorable option.forEach
, performance considerations are critical, especially in performance-sensitive applications. Understanding the underlying mechanics is valuable.forEach
), while other cases may demand the highest performance (e.g., tight loops in performance-critical applications).For scenarios requiring complex iterations, there are alternatives including:
In conclusion, the choices between these loop constructs often come down to a trade-off between readability and performance, influenced by the specific requirements of the code being written.