sort vs for in array summing

JavaScript performance comparison

Test case created by Steve Adams

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var sumTwoLargestNumbersSORT = function(numbers) {
    if (numbers.length < 2) {
      throw new Error('Expected an array with at least 2 elements.');
    }
  
    numbers = numbers.sort(function(a, b) {
      return b - a;
    });
  
  
    return numbers[0] + numbers[1];
  };
  
  var sumTwoLargestNumbersFOR = function(numbers) {
    var largest,
      secondLargest,
      length = numbers.length,
      i = 2;
  
    if (length < 2) {
      throw new Error('Expected an array with at least 2 elements.');
    }
  
    largest = numbers[0];
    secondLargest = numbers[1];
  
    if (largest < secondLargest) {
      largest = numbers[1];
      secondLargest = numbers[0];
    }
  
    for (; i < length; i++) {
      if (numbers[i] > largest) {
        secondLargest = largest;
        largest = numbers[i];
      } else if (numbers[i] > secondLargest) {
        secondLargest = numbers[i];
      }
    }
  
    return largest + secondLargest;
  };
  
  testArray = [2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8, 2, 4, 9, 24, 2, 8, 29, 0, -93, 28, -1, 0, 18, 33, 2, 5, 8];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array.prototype.sort()
sumTwoLargestNumbersSORT(testArray);
pending…
for loop
sumTwoLargestNumbersFOR(testArray);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments