for loop if oh yeah

JavaScript performance comparison

Test case created by jack

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>
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

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 loop
for(var i in q){
  if(q[i]) document.getElementById(i).className += q[i];
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';

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment