pre-allocated arrays

JavaScript performance comparison

Revision 44 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    predefinedCorrectLengthArray = new Array(10);
};
</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
empty literal
var arr = [];
for (var i = 0; i < 10; i++) {
    arr[i] = i;
}
pending…
pre-allocated array (sparse)
var arr = new Array(10);
for (var i = 0; i < 10; i++) {
    arr[i] = i;
}
pending…
pre-allocated array (dense)
var arr = Array.apply(null, new Array(10));
for (var i = 0; i < 10; i++) {
    arr[i] = i;
}
pending…
pre-allocated array - omit new keyword
var arr = Array(10);
for (var i = 0; i < 10; i++) {
    arr[i] = i;
}
pending…
empty literal, push()
var arr = [];
for (var i = 0; i < 10; i++) {
    arr.push(i);
}
pending…
clone array and replace
var arr = predefinedCorrectLengthArray.slice();
for (var i = 0; i < 10; i++) {
    arr[i] = i;
}
pending…
Pre-allocate, dynamic length
var l = 2 * 5;
var arr = new Array(l);
for (var i = 0; i < 10; i++) {
    arr[i] = 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