for..in loop if oh yeah

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<div id='a'>
</div>
<div id='b'>
</div>
<div id='c'>
</div>
<div id='d'>
</div>
<div id='e'>
</div>
<div id='f'>
</div>
<div id='g'>
</div>
<div id='h'>
</div>
<div id='i'>
</div>
<div id='j'>
</div>
<div id='k'>
</div>
<script>
Benchmark.prototype.setup = function() {
    var q = {
      a: true,
      b: true,
      c: false,
      d: true,
      e: false,
      f: false,
      g: true,
      h: true,
      i: false,
      j: false,
      k: false
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for..in loop
for (var i in q) {
  if (q[i]) document.getElementById(i).className += q[i];
}
pending…
bunch of ifs
if (q.a) document.getElementById('a').className += 'true';
if (q.b) document.getElementById('b').className += 'true';
if (q.c) document.getElementById('c').className += 'true';
if (q.d) document.getElementById('d').className += 'true';
if (q.e) document.getElementById('e').className += 'true';
if (q.f) document.getElementById('f').className += 'true';
if (q.g) document.getElementById('g').className += 'true';
if (q.h) document.getElementById('h').className += 'true';
if (q.i) document.getElementById('i').className += 'true';
if (q.j) document.getElementById('j').className += 'true';
if (q.k) document.getElementById('k').className += 'true';
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