ArrayShift

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var array_full = [0,1,2,3,4,5,6,7,8,9];
    var array_first = [0,1,2,3,4,5,6,7,8,9];
    delete array_first [0];
    delete array_first [1];
    var array_last= [0,1,2,3,4,5,6,7,8,9];
    delete array_last[7];
    delete array_last[8];
    delete array_last[9];
    var array_mix = [0,1,2,3,4,5,6,7,8,9];
    delete array_mix [2];
    delete array_mix [5];
    delete array_mix [8];
   
    function shift_old(array) {
        var temp = array;
        var newkey = 0;
        var new_array= new Array();
        for (var key = 0, len = temp.length; key < len; key++) {
                if (!temp[key]) continue;
                new_array[newkey] = temp[key];
                new_array[newkey]['key'] = newkey;
                newkey++;
        }
        return new_array;
    }
   
    function shift_new(array) {
        for (var key = 0, len = array.length; key < len; key++) {
                if(!array[key]) {
                        var newkey = key;
                        for (key++; key < len; key++) {
                                var val = array[key];
                                if(val) {
                                        array[newkey] = val;
                                        newkey++;
                                }
                        }
                        array.length = newkey;
                        break;
                }
        }
    }
};
</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
original
var array = shift_old(array_last);

 
pending…
optimised
var array = array_last;
shift_old(array);

 
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