
Don’t pay the for-loop tax - bhalp1
https://dev.to/danhomola/dont-pay-the-for-looptax
======
kennethkl
why are all the solutions to for-loop, a different kind of for-loop? aren't
you still paying the tax?

~~~
joezydeco
Right. Show me the compiled object code and tell me how much faster it is.

" _Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it._ "

— Brian W. Kernighan and P. J. Plauger in The Elements of Programming Style.

~~~
hdhzy
Well if you're comparing for loop to reduce or map in JavaScript then for will
always win as it is a built in language construct and reduce involves costly
callbacks. Interesting that the difference is only 7% on my Chrome for Android
[0].

[0]: [https://jsperf.com/foreach-vs-reduce-vs-for-
loop](https://jsperf.com/foreach-vs-reduce-vs-for-loop)

For loops have also an advantage that they can be used with yield and await
(generators and async functions) while you cannot place these keywords inside
a callback function.

~~~
mdeltito
Regarding your last point, this is also a nice benefit of for...of loops in
JS, where you can use yield and await. It's sort of the best of both worlds.

~~~
hdhzy
Oh yes, exactly! I wonder why for of was not mentioned in the article...

