You are reading a single comment by @MaBe and its replies. Click here to read the full conversation.
  • 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.

    VERSION: "2v05",
    GIT_COMMIT: "990dac35",
    BOARD: "PICO_R1_3",
    

    Test code snipped with for, while and do.

    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.

About

Avatar for MaBe @MaBe started