Array preallocation performance

JavaScript performance comparison

Revision 2 of this test case created by Valerio

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    window.growFactor = 1000;
};
</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 Fill (1x)
var arr = [],
    l = window.growFactor;
arr.length = l;
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (1x; Not initialised))
var arr = [],
    l = window.growFactor;
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (10x)
var arr = [],
    l = 10 * window.growFactor;
arr.length = l;
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (10x; Not initialised)
var arr = [],
    l = 10 * window.growFactor;
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (10x) alt initialised
var arr = new Array(10 * window.growFactor)    
for (var i = 0; i < arr.length; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (10x) alt not initialised
var arr = new Array(),
l = 10 * window.growFactor;
 
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
pending…
for-loop Fill (1x) alt
l = window.growFactor;
var arr =  new Uint32Array(l);
   
for (var i = 0; i < l; ++i) {
  arr[i] = 0;
}
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