Packed vs. holey arrays
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var packed_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var holey_array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
delete holey_array1[0];
var holey_array2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
holey_array2[13] = 0;
var holey_array3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* hole */, 11];
function packed_sum() {
var sum = 0;
for (var i = 0; i < packed_array.length; i++) {
sum += packed_array[i];
}
}
function holey_sum1() {
var sum = 0;
for (var i = 1; i < holey_array1.length; i++) {
sum += holey_array1[i];
}
}
function holey_sum2() {
var sum = 0;
for (var i = 0; i <= 10; i++) {
sum += holey_array2[i];
}
sum += holey_array2[13];
}
function holey_sum3() {
var sum = 0;
for (var i = 0; i < 10; i++) {
sum += holey_array3[i];
}
sum += holey_array3[11];
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Packed array sum |
|
pending… |
Holey array sum 1 |
|
pending… |
Holey array sum 2 |
|
pending… |
Holey array sum 3 |
|
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
- Revision 2: published
- Revision 3: published
- Revision 4: published by Yu-Cheng Chuang
- Revision 5: published by Yu-Cheng Chuang
- Revision 6: published
- Revision 7: published by Volkan Ozcelik
- Revision 8: published by Hyzhak
0 comments