Adding to array (v3)

Revision 3 of this benchmark created by Veerababu on


Description

To test different ways to push >1000 numbers into an array

Setup

var a = [];

Test runner

Ready to run.

Testing in
TestOps/sec
for loop with splice
for (var i = 1900; i < 3000; i++) {
  a.splice(a.length, 0, i);
}
ready
do while with splice
var i = 1900;
var diff = 3000 - i;
do {
 a.splice(a.length,0,i);
} while (a.length < diff);
ready
do while with length var with splice
var i = 1900;
var diff = 3000 - i;
do {
  a.splice(a.length, 0, i++);
} while (3000 - i);
ready
foreach with add
a = [3000 - 1900];
a.forEach(function(year, index) {
  a[index] = index + 1900;
});
ready
for loop with push
for (var i = 1900; i < 3000; i++) {
  a.push(i++);
}
ready
do while with push
var i = 1900;
var diff = 3000 - i;
do {
 a.push(i++);
} while (a.length < diff);
ready
do while with length
var i = 1900;
var diff = 3000 - i;
do {
  a.splice(a.length, 0, i++);
} while (3000 - i);
ready

Revisions

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

  • Revision 3: published by Veerababu on