querySelectorAll class names vs attributes

JavaScript performance comparison

Test case created by girlie_mac

Info

Curious to see if there is any perf diff between querySelectorAll('.cat.bobtail') and querySelectorAll('[data-cat="bobtail"]'), because I would rather use more meaningful data- attribute than some arbitrary class names.

Preparation code

<section id="test">
</section>
<script>
Benchmark.prototype.setup = function() {
    var section = document.getElementById('test');
    for (var i = 0; i < 500; i++) {
      var div = document.createElement('div');
      if (i % 3 === 0) {
        div.className = 'foo bar';
        div.dataset.foo = 'bar';
      } else {
        div.className = 'foo poo';
        div.dataset.foo = 'poo';
      }
   
      section.appendChild(div);
    }
};
</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
querySelectorAll class
document.querySelectorAll('.foo.bar');
pending…
querySelectorAll attribute
document.querySelectorAll('[data-foo="bar"]');
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:

2 comments

girlie_mac commented :

OMG, Opera12, you are too fast and ruining my result graph. I tried this test on O 12.12 http://jsperf.com/queryselectorall-vs-getelementsbytagname/44 and found how stupid fast Opera handles querySelectorAll.

Stefan commented :

wow..just wow :)

Add a comment