Iteration over objects vs arrays
JavaScript performance comparison
Info
Just testing the performace differences between iterating over an array and iterating over an object.
Preparation code
<script>
var a1 = [],
a2 = [],
a3 = [],
a4 = [],
ob = {},
collision = [];
for (var i = 0; i < 5000; i++) {
a1.push(i);
}
for (var i = 4000; i < 9000; i++) {
a2.push(i);
}
for (var i = 0; i < 500000; i++) {
a3.push(i);
}
for (var i = 400000; i < 900000; i++) {
a4.push(i);
}
for (var i = 0; i < 5000; i++) {
ob[i] = i;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Iterate over array |
|
pending… |
Iterate over object |
|
pending… |
Iterate over array using for..in |
|
pending… |
Standard collision test, medium array |
|
pending… |
My collision test, medium array |
|
pending… |
Standard collision test, giant array |
|
pending… |
My collision test (modified), giant array |
|
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 by Tim
0 comments