Sparse arrays vs full arrays (v29)

Revision 29 of this benchmark created on


Setup

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();
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Sparse array sum, forEach
sum = 0;

sparse.forEach(function(i, el) {
  sum += el;
});
ready
Full array sum, forEach
sum = 0;

full.forEach(function(i, el) {
  sum += el;
});
ready
Sparse array sum, for (;;)
sum = 0;

for (i = 0, l = sparse.length; i < l; i++) {
  sum += sparse[i] || 0;
}
ready
Full array sum, for (;;)
sum = 0;

for (i = 0, l = full.length; i < l; i++) {
  sum += full[i];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.