Initialise array with zeros

JavaScript performance comparison

Revision 5 of this test case created

Info

Create an array of predefined length filled entirely with zeros.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // Final array will be 1,000,000 elements long
   
    var zeros = (function () {
            var i = Math.ceil(Math.random() * 1000),
                zs = [];
            while (i--) zs.push(0);
            return zs
        })(),
        total_zeros = 1e6,
        i = total_zeros - zeros.length;
   
};

Benchmark.prototype.teardown = function() {
    //if (zeros.length !== total_zeros)
    //  alert('Error: zeros.length is ' + zeros.length)
};
</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
Push
while (i--) zeros.push(0)
pending…
Index
while (i--) zeros.push(0)//zeros[--i] = 0
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