Push vs Length

Benchmark created by James Morrin on


Description

Which is faster Array.push(val) or Array[Array.length] = val?

Setup

var collector = [],
        maxLength = 10000,
        idx;

Test runner

Ready to run.

Testing in
TestOps/sec
Array.push(val)
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector.push(Math.random());
}
ready
Array[Array.length] = val
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector[collector.length] = Math.random();
}
ready
Array[idx] = val
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector[idx] = Math.random();
}
ready

Revisions

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

  • Revision 1: published by James Morrin on