Why Array.forEach is slower than for() loop in Javascript?
arrays, foreach, javascript, performance
Solution
Updated based on feedback from @BenAston & @trincot
Roughly, this is what's happening in both cases:
For loop
- Set the index variable to its initial value
- Check whether or not to exit the loop
- Run the body of your loop
- Increment the index variable
- Back to step 2
The only overhead that happens on every iteration is the check & the increment, which are very low-load operations.
forEach loop
- Instantiate the callback function
- Check if there's a next element to process
- Call the callback for the next element to process, with a new execution context (this comprises the "scope" of the function; so its context, arguments, inner variables, and references to any outer variables -- if used)
- Run the contents of your callback
- Teardown of callback function call
- Return to step 2
The overhead of the function setup & teardown in steps 3 & 5 here are much greater than that of incrementing & checking an integer for the vanilla for-loop.
That said, many modern browsers recognize & optimize forEach calls, and in some cases, the forEach might even be faster!
Problem
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.
Related problems
- Javascript efficiency: 'for' vs 'forEach'
- Why is native javascript array forEach method significantly slower than the standard for loop?
- array.forEach running faster than native iteration? How?
- Javascript efficiency: 'for' vs 'forEach'
- Why is native javascript array forEach method significantly slower than the standard for loop?