Initialise array with zeros

JavaScript performance comparison

Revision 7 of this test case created

Info

Create an array of predefined length filled entirely with zeros.

Preparation code

<script>
// Final array will be 1,000,000 elements long

var i, arr,
    zeros = (function () {
        var i = Math.ceil(Math.random() * 1000),
            zs = [];
        while (i--) zs.push(0);
        return zs
    })(),
    total_zeros = 1e6,
    num_zeros = total_zeros - zeros.length;

alert(num_zeros);

</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
i = num_zeros;
arr = zeros.slice();
while (i--) arr.push(0);
pending…
Index - reverse fill
i = num_zeros;
arr = zeros.slice();
while (i) arr[--i] = 0;
pending…
Index
i = 0;
arr = zeros.slice();
for (; i < num_zeros; arr[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