Possible Duplicate:
What loop is faster, while or for
I see in three.js that there is the common code feature in many languages:
for ( var i = 0, l = something.length; i < l; i++ ) {
do some stuff over i
}
but I read that in javascript performance can be better by using:
var i = something.length;
while(i--){
do some stuff over i
}
Does this actually improve any performance significantly? is there a reason to prefer one over the other?
No. Not reliably cross-browser (which is to say, across JavaScript implementations).
Moreover, note that in your example, your
whileloop loops backward (n to 0), theforloop you quote loops forward (0 to n). Sometimes it matters.In general, micro-optimization is rarely appropriate, and this is particularly true in the case of JavaScript, where different implementations in the wild have markedly different performance characteristics. Instead, write code to be clear and maintainable, and address specific performance issues only if they arise, and if they arise address them on your target engines.