can anyone tell me whats the reason that array.forEach is slower than for loop in javascript. Is there any particular reason.
Here's the code that I was trying to find the performance.
// Populate the base array
var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
function someFn(i) {
return i * 3 * 8;
}
Using Array.forEach :
arr.forEach(function (item){
someFn(item);
})
Using for loop :
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
I tested it on test runner . Here are the results:
As you can see Array.ForEach is 96% slower than for loop. Thanks in advance.