loop if oh yeah

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<div id='a'>
<div id='b'>
<div id='c'>
<div id='d'>
<div id='e'>
<div id='f'>
<div id='g'>
<div id='h'>
<div id='i'>
<div id='j'>
<div id='k'>
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 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