What is the fastest way to go through an array? I read a book about optimization in Javascript, it was said that the fastest way is this:
while(i--) { } I compared two ways: this and the usual cycle:
for (var i = 1e7; i > 0; i = i - 1) {} It turned out that the usual cycle runs much faster, why? The forums also wrote that the way with while is the fastest.
I checked the speed of work like this:
var i = 1e7; var t = performance.now(); while(i--) {} console.log(performance.now() - t); var t = performance.now(); for (var i = 1e7; i > 0; i = i - 1) {} console.log(performance.now() - t);
forit is generally obvious, then withwhileit is necessary to look, suddenlyiused somewhere outside ... Maybe this is the case? - pavelforEachfor example - Grundy