I noticed that the new version of JSLint doesn't like some forms of for loops. I found that to be strange, and started digging for some explanation. Under JsLint's help page, you can find this:
The most important new feature of ES6 is proper tail calls. This has no new syntax, so jsLint doesn't see it. But it makes recursion much more attractive, which makes loops, particularly for loops, much less attractive.
And this:
jsLint does not recommend use of the for statement. Use array methods like forEach instead. The for option will suppress some warnings. The forms of for that jsLint accepts are restricted, excluding the new ES6 forms.
Both of these statements left me a byte confused. I was always under the impression that for loops were the best way of looping, mainly because:
- Some iteration methods are not very well supported yet
- for loops have less 'overheard' than other iteration methods
I also read somewhere that methods like forEach
can't be optimized as well as a for loop, although I'm not sure if I should believe that. (Probably true in ECMAScript 5?)
So, an argument in favor of other iteration methods is that they are more readable, but, with tail call optimization coming into play, can they be as fast or probably faster than a for loop?
I'm assuming here that, for instance, forEach can be tail-call optimized, is this correct?
So, the main question is: How exactly does tail call optimization come into play when deciding in favor of iteration methods other than the for loop, in ECMAScript 6?
And perhaps also this: Is it right to assume that JSLInt likes other iteration methods better because they are more readable, and because, with the optimizations in ECMAScript 6, they can be just as fast as for loops? Did I interpret the statement's over at the JSLint help page correctly?
I'm aware that questions are supposed to have only one main question in them, and that I have many in there; but, I see them all as very related to each other, and so I think that answering one of them probably answers all of them.
Thanks, and sorry for the lengthy post!