I'm not entirely sure, and I think at some point the specs changed on it. The only for loops I think you can use const in are the for of and maybe for in. It's due to the nature of how that variable is scoped. Technically in a basic i<=length for loop you use the same i-variable over the course of the loop. With the other loops, each loop has its own version of the iterator. Using const is a good way to denote this technical difference in the loop, but in practice it's only really a safeguard to not change the iterator in your code. It doesn't affect anything other loop in the series to change it though. An interesting example would be to run each loop logging the iterator from a settimeout function. For 5 iterations the i-loop would log 4 5 times whereas the for of would log 0,1,2,3,4; because it is technically logging each loops unique iterator (sorry on mobile)
It's not a matter of milliseconds it's that it's an entire magnitude slower. Sure if you're just doing a single foreach loop then it is what it is. But if you're doing a "common pattern" of filter -> map I've seen all over code bases. And then you do that in 10 different places on a front-end application, all at page load time. All of a sudden you've added an entire second to time-to-interactive.
I get the whole maintainability thing. And there are plenty of places that it ends up being much cleaner and easier to maintain using map/filter/reduce/foreach. And in some places you can add functionality with callback chains or async-await.
But it's something to be aware of that I've seen many more developers just use Array functions as the "default" without thinking.
0
u/[deleted] Dec 16 '18
[deleted]