Just wondering what is the difference in the following 2 methods?
var a = 0;
var b = 0;
var c = 0;
for(var i = 0; i < 6; i++ ){
a+=i;
b+=i;
c+=i;
}
and
var a = 0;
var b = 0;
var c = 0;
for(var i = 0; i < 6; i++ ){
a+=i;
}
for(var i = 0; i < 6; i++ ){
b+=i;
}
for(var i = 0; i < 6; i++ ){
c+=i;
}
*edited thanks locrizak for the correction
The second one is doing 3X the amount of iterations it needs to. In the second one there are 18 iterations through the loops while the first there is only 6 making the script run faster. (In these circumstances you will not notice a difference because you are not doing much in the loops but once you want to do more it will be a performance issue)
Ps. a+i isn;’t doing anything you probably want a+=i