Array direct assignment vs push (v6)

Revision 6 of this benchmark created by Jesse Dailey on


Setup

var array = [];
    var other_array = 0;
    var count = 100;

Test runner

Ready to run.

Testing in
TestOps/sec
Direct assignment
for(var i = 0; i < count; i++)
{
  array[i] = Math.random();
}
ready
Push
for(var i = 0; i < count; i++)
{
  array.push(Math.random());
}
ready
Direct assignment (calculated)
for(var i = 0; i < count; i++)
{
  array[array.length] = Math.random();
}
ready
other
// It makes no sense for this to be 20x slower,
// since the the only difference is one extra increment.
// Something is wrong with this benchmark.
for(var i = 0; i < count; i++) {
  array[other_array++] = Math.random();
}
// This is some inefficiency in jsperf (a hidden closure?)
// not the test code.
 
ready
other2
// This is what the 'other' test _meant_ to measure.
var j = 0;
for( var i = 0; i < count; i++) {
  array[j++] = Math.random();
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.