Most of the time a for loop takes up is spent doing logic on each iteration. The amount of time it takes to switch from the end of one iteration to another (which is the only way these differ) is microscopic in comparison.
Unless you're doing many thousands of iterations, performance is the wrong heuristic to help you decide
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I really hoped this was going to tell me how to write the fastest for loop! Which one is quickest?
jsperf.com/loop-vs-map-vs-foreach/22
So,
map
and classical for loop. But it is usually more semantically to useforEach
rather thanmap
, albeit slow.for loop
isn't so bad, but it depends on the use case.Why is it more semantical to write "arr.forEach(...)" instead of "for (... of arr)" ? Seems merely a matter of syntax and taste to me ...
I would absolutely consider this to be a matter of taste
I meant
arr.forEach()
vsarr.map()
.for of
andfor in
are another species, and is whether you want it to be functional or procedural. Neither is better than the others.That depends what you're measuring
Most of the time a for loop takes up is spent doing logic on each iteration. The amount of time it takes to switch from the end of one iteration to another (which is the only way these differ) is microscopic in comparison.
Unless you're doing many thousands of iterations, performance is the wrong heuristic to help you decide