Sparse arrays vs full arrays

JavaScript performance comparison

Revision 11 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var n, N, i, I, sparse = [],
      full = [],
      full2 = [];
  
  N = 16384;
  I = 8;
  
  for (i = 0; i < I; i++) {
    do {
      n = (N * Math.random()) | 0;
      if (n > (N - 1)) {
        n = N - 1;
      }
    } while (sparse[n]);
    sparse[n] = -100 + 200 * Math.random();
  }
  full.length = N;
  full2.length = N;
  for (i = 0; i < N; i++) {
    if (typeof sparse[i] === "number") {
      full[i] = sparse[i];
      full2[i] = sparse[i];
    } else {
      full[i] = 0;
      full2[i] = -100 + 200 * Math.random();
    }
  }
  
  
  console.log(sparse);
  console.log(full);
  console.log(full2);
  

};
</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
Sparse array sum
var sum = 0;
sparse.forEach(function(v, i) {
  sum += v;
});
pending…
Full array sum
var len = full.length;
sum = 0;
for (i = 0; i < len; i++) {
  sum += full[i];
}
pending…
Running sparse on full array
var sum = 0;
full.forEach(function(v, i) {
  sum += v;
});
pending…
Sum all elements on a full array without zeros
var len = full2.length;
sum = 0;
for (i = 0; i < len; i++) {
  sum += full2[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.

0 Comments