Iterating though array vs object of array values

JavaScript performance comparison

Test case created by Mourner

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [1, 2, 3, 4, 5, 6, 7];
    var b = {a: [1], b: [2], c: [3], d: [4], e: [5], f: [6], g: [7]};
   
    var sum = 0, i, j, len;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
array
for (i = 0, len = a.length; i < len; i++) {
  sum += a[i];
}
pending…
object of arrays
for (i in b) {
  if (b.hasOwnProperty(i)) {
    for (j = 0, len = b[i].length; j < len; j++) {
      sum += b[i][j];
    }
  }
}
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:

0 comments

Add a comment