- A is executed once before starting the loop.
- B is reevaluated before each iteration, and if it is not, it exits the loop (therefore, it checks the length
Things property on each iteration.) - C is executed after each iteration
However, the performance that you get from changing the loop is minimal, and you risk sacrificing some of the readability, so stick to what you consider to be the most readable and not what is the most correct in terms of performance.
This may make more sense to you:
for (var i=0; i < Things.length; i++) { Things[i] = null; };
can be rewritten as follows:
var i = 0; //A while (true) { if (!(i < Things.length)) { //B - We check the length all the time break; } Things[i] = null; i++; //C }
and
for (var i = Things.length - 1; i >= 0; i--){ Things[i] = null; };
can be rewritten as follows:
var i = Things.length - 1; //A - We only check the length once while (true) { if (!(i >= 0)) { //B break; } Things[i] = null; i--; //C }
h2ooooooo Jul 05 '13 at 8:44 2013-07-05 08:44
source share