javascript speed test

given the following function:

  function addInts(anArray){
     var result = 0;
     for (var i = 0; i < anArray.length; ++i){
        if (anArray[i] < 21) {
           result = result + anArray[i];
        } else { 
           result = result + anArray[i] - 21;
        }
     }
     return result;
  }

what would you expect will run faster?

    addInts(sortedArray); #sorted
    addInts(unsortedArray); #unsorted

 


 
check your answer


 

[More programming riddles]

Leave a comment