Array direct assignment vs push

JavaScript performance comparison

Revision 20 of this test case created by vincent piel

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var array = [];
    var array2 = [];
    array2.len = 0;
    var other_array = 0 ;
    var count = 100;
    var array_push = function (arr, item) {
       arr[arr.length] = item;
    };
};
</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
Direct assignment
for(var i = 0; i < count; i++)
{
  array[i] = i;
}
array[count] = (count = count + 200);
count = count % 200;
pending…
Push
for(var i = 0; i < count; i++)
{
  array.push(i);
}
array[count] = (count = count + 200);
count = count % 200;
pending…
Direct assignment (calculated)
for(var i = 0; i < count; i++)
{
  array[array.length] = i;
}
array[count] = (count = count + 200);
count = count % 200;
pending…
other
for(var i = 0; i < count; i++)
{
  array[other_array++] = i;
}
array[count] = (count = count + 200);
count = count % 200;
pending…
array2.len
for(var i = 0; i < count; i++)
{
  array2[array2.len++] =i;
}
array[count] = (count = count + 200);
count = count % 200;
pending…
Direct assignment via function
for (var i = 0; i < count; ++i) {
   array_push(array2,i);
}
array[count] = (count = count + 200);
count = count % 200;
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