Sparse arrays vs full arrays
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var n, N, i, I, sparse = [], sparse2 = [], sparse3 = [],
full = [],
full2 = [];
N = 32;//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") {
sparse2[i] = sparse[i];
sparse3[i] = sparse[i];
full[i] = sparse[i];
full2[i] = sparse[i];
} else {
sparse2[i] = undefined;
sparse3[i] = null;
full[i] = 0;
full2[i] = 100 + 200 * Math.random();
}
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test  Ops/sec  

Sparse array sum 

pending… 
Full array sum 

pending… 
Running sparse on full array 

pending… 
Sum all elements on a full array without zeros 

pending… 
for loop over sparse 

pending… 
for loop over fauxsparse 

pending… 
for loop over fauxsparse (null) 

pending… 
loop over full (with 0 check) 

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:
 Revision 1: published by Mattias Ernelli
 Revision 3: published
 Revision 6: published by Jasmine
 Revision 7: published
 Revision 8: published by Andreas Öman
 Revision 9: published
 Revision 10: published
 Revision 11: published
 Revision 12: published
 Revision 16: published by Lee Byron
 Revision 19: published
 Revision 21: published
 Revision 22: published
 Revision 23: published
 Revision 24: published
 Revision 25: published by Tom
 Revision 26: published by Volodymyr Vadyak
 Revision 27: published by Volodymyr Vadyak
 Revision 28: published
 Revision 29: published
0 comments
Comment form temporarily disabled.