Array Sorting Puzzler

JavaScript performance comparison

Test case created by Mihai Parparita

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var kItemCount = 10000;
  var array1 = [];
  var array2 = [];
  var array3 = [];
  for (var i = 0; i < kItemCount; i++) {
    array1.push([i, [0]]);
    array2.push([i, [0]]);
    array3.push(i);
  }
  
  shuffle(array2);
  
  var array1Copy = array1.slice(0);
  var array2Copy = array2.slice(0);
  var array3Copy = array3.slice(0);
  
  function shuffle(array) {
    var currentIndex = array.length;
    while (currentIndex) {
      var randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex--;
      var temp = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temp;
    }
  }

};
</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
Sort array1
array1.sort()
pending…
Sort array2
array2.sort()
pending…
Sort array3
array3.sort()
pending…
Sort array1 with comparator
array1Copy.sort(function (a, b) {
  return a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0);
});
pending…
Sort array2 with comparator
array2Copy.sort(function (a, b) {
  return a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0);
});
pending…
Sort array3 with comparator
array3Copy.sort(function (a, b) {
  return a < b ? -1 : (a > b ? 1 : 0);
});
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