querySelectorAll class names vs attributes
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
querySelectorAll class |
|
pending… |
querySelectorAll attribute |
|
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:
- Revision 1: published by girlie_mac
- Revision 2: published by Jim Montgomery
- Revision 3: published by Jim Montgomery
2 comments
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.
wow..just wow :)