array filling speed

JavaScript performance comparison

Revision 3 of this test case created

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
var arr = [];

for ( var i = 0; i < 1000; i++ ) {
    arr.push(i);
}
pending…
[i] = i
var arr = [];

for ( var i = 0; i < 1000; i++ ) {
    arr[i] = i;
}
pending…
pre filled array, [].push
var arr = new Array(1000);

for ( var i = 0; i < 1000; i++ ) {
    arr.push(i);
}
pending…
pre filled array, [i] = i
var arr = new Array(1000);

for ( var i = 0; i < 1000; i++ ) {
    arr[i] = i;
}
pending…
[] with length pre-set, [].push
var arr = [];
arr.length = 1000;

for ( var i = 0; i < 1000; i++ ) {
    arr.push(i);
}
pending…
[] with length pre-set, [i] = i
var arr = [];
arr.length = 1000;

for ( var i = 0; i < 1000; i++ ) {
    arr[i] = i;
}
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:

1 comment

kubelsmieci commented :

When array is not initialized, arr.push(i) is faster than arr[i] = i; otherwise, arr[i] = i is faster than push.

Add a comment