bubblesort2way

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function swap(items, firstIndex, secondIndex){
        var temp = items[firstIndex];
        items[firstIndex] = items[secondIndex];
        items[secondIndex] = temp;
    }
   
    function bubbleSort2(items){
        var len = items.length,
            i, j;
   
        for (i=len-1; i >= 0; i--){
            for (j=len-i; j >= 0; j--){
                if (items[j] < items[j-1]){
                    swap(items, j, j-1);
                }
            }
        }
   
        return items;
    }
   
    function bubbleSort(items){
   
        var len = items.length,
            i, j, stop;
   
        for (i=0; i < len; i++){
            for (j=0, stop=len-i; j < stop; j++){
                if (items[j] > items[j+1]){
                    swap(items, j, j+1);
                }
            }
        }
   
        return items;
    }
   
    var a = [6,5,3,7,3,7,8,2,4,0];
};
</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
1
bubbleSort(a);
pending…
2
bubbleSort2(a);
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