2

JavaScript’s Array.prototype.forEach is usually significantly slower than a for loop. This is because forEach includes extra checks that a for loop normally doesn’t have. Are there any cases where a forEach loop works (because of these checks) where a for loop doesn’t?

To be clear, I am not asking why for is faster than forEach. I am asking if the extra checks in forEach are ever useful.

AlexH
  • 828
  • 7
  • 26
  • what extra checks are you talking about? In most practical cases, you are unlikely to see major performance differences in your application between `for` and `forEach`. – david_adler Oct 23 '20 at 13:30
  • See [this question](https://stackoverflow.com/questions/22155280/why-is-native-javascript-array-foreach-method-significantly-slower-than-the-stan). It shows the extra checks in the `forEach` implementation. – AlexH Oct 23 '20 at 13:32
  • I am aware there are rarely major performance differences; I'm just curious about the use cases of `forEach` as opposed to `for`. – AlexH Oct 23 '20 at 13:33
  • Array methods are preferred over for loops because they express their intent more clearly. You can read the following articles: [loops-are-evil.md](https://gist.github.com/robotlolita/7643014) and [Can we say that the "for loop" will be the new "goto,” given the increasing popularity of functional programming?](https://www.quora.com/Can-we-say-that-the-for-loop-will-be-the-new-goto-%E2%80%9D-given-the-increasing-popularity-of-functional-programming/answer/Tikhon-Jelvis?share=1) – Moritz Roessler Oct 23 '20 at 13:42
  • One thing that `forEach()` can't do is [`skip array indexes`](https://stackoverflow.com/questions/64100018/how-can-i-change-step-of-index-in-foreach-method-at-javascript) – Yousaf Oct 23 '20 at 13:42
  • @Yousaf You can get around that by checking the index inside the function, doing a noop. To emulate a `break` you'd have to set a flag. You'd still have to iterate over every element though. If you need to break out of a nested loop, you'd need to return a magic value. – Moritz Roessler Oct 23 '20 at 13:49
  • @MoritzRoessler yes you can get around that but still you aren't skipping indexes. And also you can't stop `.forEach()` from executing from start till end whereas a `for` loop can be terminated using the `break` keyword. – Yousaf Oct 23 '20 at 13:51
  • @Yousaf Yes, of course. – Moritz Roessler Oct 23 '20 at 13:59

3 Answers3

3

There are several differences between forEach and using a for loop

The most obvious is how they deal with sparse arrays. forEach wont try to run the callback if it hits an empty array element but for will and the array item will give undefined.

On the other side, you can’t just break out of a forEach as you can a for loop.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach for further details.

A Haworth
  • 30,908
  • 4
  • 11
  • 14
2

forEach() skip empty elements (holes) in the array, for do not.

Artem Matiushenko
  • 814
  • 10
  • 14
0

You could make the for loop as slow as you want by changing the traditional increment to a more complex operation. It also has to do an inequality check during each iteration, so that is another opportunity to introduce delays, by using a more complex condition.

forEach is very specific, but for can offer more flexibility.

mike.k
  • 3,277
  • 1
  • 12
  • 18
  • Right. But are there any scenarios where `forEach` can do something that `for` can't? – AlexH Oct 23 '20 at 13:36
  • No, and that's confirmed by the polyfill answer you linked to. And those checks happen up-front, not inside the loop. – mike.k Oct 23 '20 at 13:37