Array init

JavaScript performance comparison

Test case created by kamil-kielczewski

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let n = 100000;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
A
let a = Array(n).fill(0);
pending…
B
let a = new Array(n).fill(0);
pending…
C
let a = Array.from({ length: n }, () => 0);
pending…
D
let a = [...new Array(n)].map(x => 0);
pending…
E
let a = [...new Array(n)].fill(0);
pending…
F
const arr = [];
arr.length = n;
let i = 0;
while (i < n) {
  arr[i] = 0;
  i++;
}
pending…
G
let a=[]; for(i=n;i--;)a[i]=0;
pending…
H
let a = "0".repeat( n ).split("").map( parseFloat );
pending…
I
let a = Array.from(new Int32Array(n));
pending…
J
let a = Array.from(new Float32Array(n));
pending…
K
let a = Array.from(new Float64Array(n)); // needs 2x more memory than float32
pending…
L
let a = [].slice.apply(new Float32Array(n));
pending…
M
let a = new Float32Array(n); // this is not "usual" array in fact (we cannot add new elements, but it is efficient and worth to know it
pending…
N
let a = Array.apply(null, Array(n)).map(Number.prototype.valueOf,0);
pending…
O
let a = new Array(n+1).join('0').split('').map(parseFloat)
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.

0 Comments