Can someone tell me that the reason array.forEach is slower than for a loop in javascript. Is there any special reason.
Here is the code I was trying to find.
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);
})
Usage for the loop:
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
I tested it on a tester. Here are the results:

As you can see, Array.ForEach is 96% slower than for a loop. Thanks in advance.
source
share