make filled array

JavaScript performance comparison

Test case created

Info

Compare speeds of native array methods vs a while loop

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function spliceMap(length, fill){
        var a = [], b = [];
        a.length = length + 2;
            a[0] = 0; a[1] = 0;
        b.splice.apply(b,a);
        return b.map(function(){return fill;});
    }
   
    function pushMap(length, fill){
        var a = [], b = [];
        a.length = length;
        b.push.apply(b,a);
        return b.map(function(){return fill;});
    }
   
    function whileLengthNew(len, val) {
        var rv = new Array(len);
        while (--len >= 0) {
            rv[len] = val;
        }
        return rv;
    }
   
    function whileLengthLiteral(len, val) {
        var rv = [];
        while (--len >= 0) {
            rv[len] = val;
        }
        return rv;
    }
};
</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
spliceMap
spliceMap(255,0);
spliceMap(255,true);
pending…
pushMap
pushMap(255,0);
pushMap(255,true);
pending…
whileLengthLiteral
whileLengthLiteral(255,0);
whileLengthLiteral(255,true);
pending…
whileLengthNew
whileLengthNew(255,0);
whileLengthNew(255,true);
pending…
BIG pushMap
pushMap(65536,0);
pushMap(65536,true);
pending…
BIG whileLengthNew
whileLengthNew(65536,0);
whileLengthNew(65536,true);
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