-
• #2
It's not an entirely fair comparison if you've got
x=0
in the for loop but not in any other loops, but I believefor
will be slower anyway, just because Espruino is having to jump between 3 different places (the loop iterator, condition and body)...The fastest would be:
var i = LOOPS; while (i--) { }
-
• #3
Well not sure about this, isn't
x=0
faster than `i++, but let's compare the empty loops.function forLoopV2(){ for (i = 0; i < LOOPS; i++) {} } function whileLoopV2(){ var i = LOOPS; while (i--) {} }
>run(forLoopV2) =1.96909236907 >run(whileLoopV2) =0.91626548767
Any way it is clear that while loops are faster than for loops.
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.