// ( A ) ( B ) (C)
for (var i=0; i < Things.length; i++) {
Things[i]
};
- A is executed once before the loop starts.
- B is re-evaluated before every iteration, and if it's not true, it exits the loop (hence it checks the length property of
Things
on every single iteration.)
- C is executed after every iteration
That said, the performance you get from changing the loop is minimal, and you risk sacrificing part of the readability, so stick with what you find most readable - not what is most correct performance-wise.
This might make more sense for you:
for (var i=0; i < Things.length; i++) {
Things[i] = null;
};
could be rewritten as the following:
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;
};
could be rewritten as the following:
var i = Things.length - 1; //A - We only check the length once
while (true) {
if (!(i >= 0)) { //B
break;
}
Things[i] = null;
i--; //C
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…