<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
let test = 0;
function globalMeasureThatScriptPrepareFunction() {
test = Math.random();
}
const ar = Array(100000);
for (let i= 0; i < ar.length; i++) {
globalMeasureThatScriptPrepareFunction();
}
ar.forEach(() => {
globalMeasureThatScriptPrepareFunction();
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
FOR | |
FOREACH |
Test name | Executions per second |
---|---|
FOR | 1534.8 Ops/sec |
FOREACH | 19818.9 Ops/sec |
The benchmark defined in the provided JSON compares two different looping techniques in JavaScript: the traditional for
loop and the forEach
method. Both of these methods are used to iterate over elements in an array (ar
), which contains 100,000 entries, but they do it in different ways.
FOR Loop:
for (let i= 0; i < ar.length; i++) {
globalMeasureThatScriptPrepareFunction();
}
for
loop is straightforward and a commonly used iteration method.forEach Method:
ar.forEach(() => {
globalMeasureThatScriptPrepareFunction();
});
forEach
method is more concise and can improve readability. It's clear that you are performing an operation on each item in the array without managing loop counters.forEach
method does not support breaking out of the loop or using return
to exit early (though this can be mitigated with some
or every
in some scenarios).for
loop, as it introduces a function scope with each iteration.In this benchmark, there is no external library being used. The code operates using standard JavaScript features. The function globalMeasureThatScriptPrepareFunction
is defined in the Script Preparation Code and is executed within both test cases to account for any asynchronous or additional processing that would affect the timing of iterations.
No special JavaScript features are used that require explanation beyond standard looping constructs. Both for
and forEach
methods are considered core JavaScript syntax.
The benchmark results show a significant difference in execution speed between the two methods:
This demonstrates that, in this particular case and environment (using Opera 117 on Windows Desktop), the forEach
method outperforms the traditional for
loop by a large margin.
Other alternatives to iterating through an array in JavaScript include:
for
loop: As analyzed above, suitable for performance-critical sections.for...of
loop: Provides a clear and concise way to iterate over iterable objects (like arrays) while maintaining readability.for (const item of ar) {
globalMeasureThatScriptPrepareFunction();
}
map
or reduce
: These methods are also used to iterate through arrays while performing transformations or aggregations, though they're primarily for creating new arrays/types, which is not applicable for simple iteration.Ultimately, in scenarios where performance and handling of large datasets are critical, developers might choose traditional loops, while readability and ease of use might make forEach
preferable in other contexts. Each method has its advantages and should be selected based on the specific requirements of the task at hand.