quicksort vs native

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var list = [];
    for (var i = 0; i < 10000; i++) {
        list.push(Math.floor(Math.random()*i));
    }
   
    function quickSort(arr){
                if (arr.length <= 1)
                {
                    return arr;
                }
                var pivotIndex = Math.floor(arr.length / 2) ;
                var pivot = arr.splice(pivotIndex, 1)[0];
                var left = [];
                var right = [];
                for (var i = 0; i < arr.length; i++){
                    if (arr[i] < pivot) {
                        left.push(arr[i]);
                    } else {
                        right.push(arr[i]);
                    }
                }
                return quickSort(left).concat([pivot], quickSort(right));
            }
};
</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
quicksort
quickSort(list)
pending…
native
list.sort(function(a, b){
    return a - b;
});
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

Add a comment