Array direct assignment vs push

JavaScript performance comparison

Revision 10 of this test case created by

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Direct assignment
var array = [],
    other_array = 0,
    count = 100,
    i;

for (i = 0; i < count; i++) {
  array[i] = Math.random();
}
pending…
Push
var array = [],
    other_array = 0,
    count = 100,
    i;

for (i = 0; i < count; i++) {
  array.push(Math.random());
}
pending…
Direct assignment (calculated)
var array = [],
    other_array = 0,
    count = 100,
    i;

for (i = 0; i < count; i++) {
  array[array.length] = Math.random();
}
pending…
other
var array = [],
    other_array = 0,
    count = 100,
    i;

for (i = 0; i < count; i++) {
    array[other_array++] = Math.random();
}
pending…
cache method
var array = [],
    other_array = 0,
    count = 100,
    i;

for (i = 0; i < count; i++) {
  array[i] = Math.random();
}
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.

0 Comments