a.push vs. a[a.length] (v5)

Revision 5 of this benchmark created by Vladimir Sitnikov on


Description

I heard Chrome managed to increase the performance of Array.prototype.push to such heights it actually ran faster than assigning to a[a.length].

Turns out it seems to be the case (provided the benchmarks and benchmarking tools are correct, of course).

Proves the point that premature and/or micro optimization targeting multiple evolving platforms can be even more than just a waste of time: it can be totally counterproductive.

Test runner

Ready to run.

Testing in
TestOps/sec
a.push
var a = [1, 2, 3],
    b = 4;
for (var i = 3; i < 1000; i++) {
 a.push(b);
}
ready
a.p
var a = [1, 2, 3],
    b = 4;
Array.prototype.p = Array.prototype.push;
for (var i = 3; i < 1000; i++) {
 a.p(b);
}
ready
a[a.length]
var a = [1, 2, 3],
    b = 4;
for (var i = 3; i < 1000; i++) {
 a[a.length] = b;
}
ready
a[i++]
var a = [1, 2, 3],
    b = 4,
    i = 3;
for (var j = 3; j < 1000; j++) {
 a[i++] = b;
}
ready

Revisions

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

  • Revision 1: published by Tobie Langel on
  • Revision 2: published by Tobie Langel on
  • Revision 4: published by WebReflection on
  • Revision 5: published by Vladimir Sitnikov on