Array direct assignment vs push

JavaScript performance comparison

Revision 8 of this test case created by Bill Barry

Test runner

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

Java applet disabled.

Testing in unknown unknown
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,
    rand = Math.random,
    i;

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