Array direct assignment vs push

JavaScript performance comparison

Revision 6 of this test case created by Jesse Dailey

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var array = [];
  var other_array = 0;
  var count = 100;

};
</script>

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
for(var i = 0; i < count; i++)
{
  array[i] = Math.random();
}
pending…
Push
for(var i = 0; i < count; i++)
{
  array.push(Math.random());
}
pending…
Direct assignment (calculated)
for(var i = 0; i < count; i++)
{
  array[array.length] = Math.random();
}
pending…
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.
pending…
other2
// This is what the 'other' test _meant_ to measure.
var j = 0;
for( var i = 0; i < count; i++) {
  array[j++] = 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