my quicksort vs array.sort

JavaScript performance comparison

Test case created by Andymensional

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Particle() {
    this.x = Math.random()*400;
    this.y = Math.random()*400;
    this.z = Math.random()*400;
    }
    function qSort(array, start, end) {
      if (start < end) {
        var pivotIndex = (start + end) >> 1;
        var pivotValue = array[pivotIndex].z;
        var tmp = array[pivotIndex];
        array[pivotIndex] = array[end];
        array[end] = tmp;
        var pivotIndexNew = start;
        for (var i = start; i < end; i++) {
          if (array[i].z < pivotValue) {
            tmp = array[i];
            array[i] = array[pivotIndexNew];
            array[pivotIndexNew] = tmp;
            pivotIndexNew++;
          }
        }
        tmp = array[pivotIndexNew];
        array[pivotIndexNew] = array[end];
        array[end] = tmp;
        qSort(array, start, pivotIndexNew-1);
        qSort(array, pivotIndexNew+1, end);
      }
    }
   
    var part1 = [];
    var part2= [];
    for (var i = 0; i <1000; i++){
    part1.push(new Particle());
    part2.push(new Particle());
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
array.sort
part1.sort(function(a,b){return a.z-b.z});
pending…
my quicksort
qSort(part2,0,999);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment