Re:JavaScript Performance MythBusters (via jsPerf)
by mistercow on Saturday March 17, 2012 @12:50PM
(#3717466)
Well, `forEach` being faster than a for loop is more of a newbie misconception I think. It's not an unreasonable assumption, after all. In fact, looking at the `forEach` algorithm[1], it is not even immediately obvious why it would be so much slower than a for loop.
It seems that the killer is the membership test[2] at line 44. What I don't really understand is why `i in O`, when O is an array, would be so slow, and furthermore, given that it is that slow, why `forEach` uses it instead of a simple bounds check.
It seems that the killer is the membership test[2] at line 44. What I don't really understand is why `i in O`, when O is an array, would be so slow, and furthermore, given that it is that slow, why `forEach` uses it instead of a simple bounds check.
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global...
[2]: http://jsperf.com/for-vs-foreach/17