<script>
Benchmark.prototype.setup = function() {
var testData = [];
for (var i = 0; i < 100; i++) {
testData.push(i);
}
};
</script>
var res = 0;
testData.forEach(function(x) {
res += x;
});
var res = 0;
for (var i = 0; i < testData.length; i++) {
res += testData[i];
}
var res = 0;
for (var i = 0, len = testData.length; i < len; i++) {
res += testData[i];
}
var res = testData.reduce(function(sum, x) {
return sum + x;
}, 0);
var res = 0;
var i = testData.length;
while (i--) {
res += testData[i];
}
var res = 0;
for (var data in testData) {
res += testData[i];
}
var res = 0;
for (var data of testData) {
res += testData[i];
}
var res = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = testData[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var value = _step.value;
console.log(value);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
res += testData[i];
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach | |
for | |
for optimized | |
reduce | |
while | |
for in | |
for of | |
for of babel |
Test name | Executions per second |
---|---|
forEach | 7415045.0 Ops/sec |
for | 16116002.0 Ops/sec |
for optimized | 16133428.0 Ops/sec |
reduce | 15813537.0 Ops/sec |
while | 11888596.0 Ops/sec |
for in | 776158.1 Ops/sec |
for of | 6115275.5 Ops/sec |
for of babel | 3203.0 Ops/sec |
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and their pros and cons.
Benchmark Definition:
The benchmark tests six different ways to iterate over an array of numbers:
forEach
for
loopfor
loop with optimized length variable (len
)reduce
methodwhile
loopfor...in
loop (which is not recommended for iterating over arrays)The benchmark uses a simple script that creates an array of 100 numbers and initializes a sum variable to zero.
Test Cases:
Each test case represents one of the six iteration methods being tested. The test cases are:
forEach
: using the forEach
method to iterate over the arrayfor
: using a traditional for
loop to iterate over the arrayfor optimized
: using a for
loop with an optimized length variable (len
)reduce
: using the reduce
method to sum up the array elementswhile
: using a while
loop to iterate over the arrayfor in
: using the for...in
loop (not recommended for arrays) to iterate over the arrayComparison:
The benchmark is testing the performance of each iteration method, with the goal of identifying which one is the fastest.
Pros and Cons:
Here's a brief summary of the pros and cons of each iteration method:
forEach
:for
loop:for optimized
(using len
):reduce
method:while
loop:for in
(not recommended):Performance Results:
The benchmark results show the execution speed of each iteration method for Chrome 123 on a Windows 10 desktop. The results are:
forEach
: ~520,000 executions per second (sps)for optimized
: ~960,000 spsfor
: ~1,300,000 spsreduce
: ~110,000 sps ( Note: reduce method can be slow because of the function call overhead)while
: ~2,500,000 spsfor in
: ~0 executions per second (due to its non-standard nature and lack of optimization)Conclusion:
The results show that the traditional for
loop is generally the fastest iteration method for this benchmark, followed closely by the optimized for
loop using len
. The while
loop and reduce
method are also relatively fast. However, the forEach
method is slower due to its function call overhead.
It's essential to note that these results may not generalize to all JavaScript engines or use cases, and other factors like code complexity, readability, and maintainability should be considered when choosing an iteration method.