Adding to array

JavaScript performance comparison

Revision 3 of this test case created by Veerababu

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [];
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for loop with splice
for (var i = 1900; i < 3000; i++) {
  a.splice(a.length, 0, i);
}
pending…
do while with splice
var i = 1900;
var diff = 3000 - i;
do {
 a.splice(a.length,0,i);
} while (a.length < diff);
pending…
do while with length var with splice
var i = 1900;
var diff = 3000 - i;
do {
  a.splice(a.length, 0, i++);
} while (3000 - i);
pending…
foreach with add
a = [3000 - 1900];
a.forEach(function(year, index) {
  a[index] = index + 1900;
});
pending…
for loop with push
for (var i = 1900; i < 3000; i++) {
  a.push(i++);
}
pending…
do while with push
var i = 1900;
var diff = 3000 - i;
do {
 a.push(i++);
} while (a.length < diff);
pending…
do while with length
var i = 1900;
var diff = 3000 - i;
do {
  a.splice(a.length, 0, i++);
} while (3000 - i);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment