Array Assignment vs Array Push (v9)

Revision 9 of this benchmark created on


Description

You've heard the rumors - now here's the proof.

v3 adds pre-allocation

Setup

var size = 2000;

Test runner

Ready to run.

Testing in
TestOps/sec
arrayA[i] = 'foo';
var arrayA = [];
for (var i = 0; i < size; i++) {
  arrayA[i] = 'foo';
}
ready
arrayB.push('foo');
var arrayB = [];
for (var i = 0; i < size; i++) {
  arrayB.push('foo');
}
ready
arrayC[i] = 'foo'; (preallocated)
var arrayC = new Array(size);
for (var i = 0; i < size; i++) {
  arrayC[i] = 'foo';
}
ready
arrayD[i] = 'foo'; (preallocated alt)
var arrayD = [];
arrayD.length = size;
for (var i = 0; i < size; i++) {
  arrayD[i] = 'foo';
}
ready
arrayE.push('foo'); (alt)
var arrayE = [];
while (arrayE.push('foo') < size) {}
ready

Revisions

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