Adding to array

JavaScript performance comparison

Revision 2 of this test case created by mahesh

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
for (var i = 1900; i < 3000; i++) {
  a.push(i);
}
pending…
do while
var i = 1900;
var diff = 3000 - i;
do {
  a.push(i++);
} while (a.length < diff);
pending…
do while with length var
var i = 1900;
var diff = 3000 - i;
do {
  a.push(i++);
} while (3000 - i);
pending…
foreach with add
a = [3000 - 1900];
a.forEach(function(year, index) {
  a[index] = index + 1900;
});
pending…
for-each index add
var a = [];
a.length = 3000 - 1900;
a[0] = 1900;
a.forEach(function(ele, index) {
  a[index + 1] = index + 1900;
});
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