countsort

JavaScript performance comparison

Test case created by Arnor

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // create an array
    var min = 0, max = 100, unsorted = [];
    for (var i = 0; i < 1000; i++) {
        unsorted.push((Math.random() * max) << 0);
    }
   
    function countSort(arr, min, max) {
        var i, z = 0, count = [];
        for (i = min; i <= max; i++) {
            count[i] = 0;
        }
        for (i=0; i < arr.length; i++) {
            count[arr[i]]++;
        }
        for (i = min; i <= max; i++) {
            while (count[i]-- > 0) {
                arr[z++] = i;
            }
        }
    }
   
    function normalSort(arr) {
        arr.sort(function(a,b) { return a - b; });
    }
};
</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
normal js sort
normalSort(unsorted);
pending…
count sort
countSort(unsorted, min, max);
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