Performance of Array vs. Object
JavaScript performance comparison
Info
After seeing http://jsperf.com/javascript-associative-vs-non-associative-arrays, I thought the test could be improved.
variant of the original test - read only
Preparation code
<script>
Benchmark.prototype.setup = function() {
var data = {},arr = [], obj = {},
i;
for(i = 0; i < 100000; i += 1) {
data[i] = {payload:i};
}
function getRandomIndex(obj) {
var idx = Math.floor(Math.random()*99999);
if(false === data.hasOwnProperty(idx)) {
while (false === data.hasOwnProperty(idx)) {
idx = Math.floor(Math.random()*9999);
}
}
return idx;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Array Performance |
|
pending… |
Object Performance |
|
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 Christopher Froehlich
- Revision 2: published by hyphen
- Revision 3: published by George
- Revision 4: published by Valeriu Paloş
- Revision 5: published
- Revision 8: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published by Marcin
- Revision 14: published by vincent piel
- Revision 15: published
- Revision 16: published
- Revision 17: published
- Revision 19: published by Jamie Olson
- Revision 21: published
- Revision 22: published
- Revision 24: published by Marcin
- Revision 25: published
- Revision 26: published
1 comment
Apparently, insertion into arrays and objects have pretty comparable performance.