Quick Sort

JavaScript performance comparison

Test case created by Gabriel R. Giannattasio

Preparation code

<script>
function newSort(L) {
  var temp,
      j;
  for (var i = 0, max = len(L); i < max - 1; i++) {
    j = i + 1;
    while (j < max) {
      if (L[i] > L[j]) {
        temp = L[i];
        L[i] = L[j];
        L[j] = temp;
      }
      j += 1;
    }
  }
}

function mySort(L) {
  var clear = false,
      temp;
  while (!clear) {
    clear = true;
    for (var j = 1, max = len(L); j < max; j++) {
      if (L[j - 1] > L[j]) {
        clear = false;
        temp = L[j];
        L[j] = L[j - 1];
        L[j - 1] = temp;
      }
    }
  }
}

function len(obj) {
  return obj.length;
}

function arrayGen(n) {
  var i, arr = [];
  for (i = 0; i < n; i++) {
    arr[i] = i + 1;
  }
  return arr;
}

var arr50 = arrayGen(50);
var arr500 = arrayGen(500);
var arr5000 = arrayGen(5000);

function rnd() {
  return Math.random() - 0.5;
}

arr50.sort(rnd);
arr500.sort(rnd);
arr5000.sort(rnd);

</script>
      
<script>
Benchmark.prototype.setup = function() {
  function clone(a) {
    var b = [];
    a.forEach(function(n) {
      b.push(n);
    });
    return b;
  }
  
  var ar50 = clone(arr50);
  var ar500 = clone(arr500);
  var ar5000 = clone(arr5000);

};
</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
mySort50
mySort(ar50);
pending…
mySort500
mySort(ar500);
pending…
mySort5000
mySort(ar5000);
pending…
newSort50
newSort(ar50);
pending…
newSort500
newSort(ar500);
pending…
newSort5000
newSort(ar5000);
pending…

Compare results of other browsers

Revisions

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

0 Comments