var LOOPS = 1E4;
function forLoop(){
for (i = 0; i < LOOPS; i++) {
x = 0;
}
}
function whileLoop(){
var i = LOOPS;
while (i) {
i--;
}
}
function doLoop(){
var i = LOOPS;
do {
i--;
} while(i);
}
function run(f){
ts = getTime();
f();
te = getTime();
return te - ts;
}
testing:
// result time is in seconds
>run(forLoop)
=2.67205524444
>run(whileLoop);
=1.25568199157
>run(doLoop);
=1.31462860107
As a result: Try to use do or while loops instead of for loops , they run nearly twice as long.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
There has been a lot talking about loop performance.
So lets's try to find out if there is a difference between loops and loops.
Tested on an original Espruino board so others may differ for the time but not for the result.
Test code snipped with for, while and do.
testing:
As a result: Try to use do or while loops instead of for loops , they run nearly twice as long.