querySelectorAll class names vs attributes

JavaScript performance comparison

Revision 3 of this test case created by Jim Montgomery

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

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="sekt_preset" sekt="preset" class="sekt_preset">
</div>
<div id="sekt_generated">
</div>
<script>
var f = document.createDocumentFragment(),
  sekt_generated = document.getElementById('sekt_generated'),
  sekt_generated_f;
f.appendChild(document.documentElement.cloneNode(1));
sekt_generated_f = f.querySelector('#sekt_generated');

</script>
<script>
Benchmark.prototype.teardown = function() {
    sekt_generated.className = sekt_generated_f.className = '';
    sekt_generated.removeAttribute('sekt');
    sekt_generated_f.removeAttribute('sekt');
};
</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
document.querySelectorAll('[sekt=preset]');
document.querySelectorAll('[sekt=preset]');
pending…
document.querySelectorAll('[sekt=generated]');
sekt_generated.setAttribute('sekt','generated');
document.querySelectorAll('[sekt=generated]');
 
pending…
document.querySelectorAll('.sekt_generated');
sekt_generated.className += ' sekt_generated';
document.querySelectorAll('.sekt_generated');
 
pending…
f.querySelectorAll('[sekt=generated]');
sekt_generated_f.setAttribute('sekt','generated');
f.querySelectorAll('[sekt=generated]');
 
pending…
f.querySelectorAll('.sekt_generated');
sekt_generated_f.className += ' sekt_generated';
f.querySelectorAll('.sekt_generated');
 
pending…
jQuery(f).find('.sekt_generated');
sekt_generated_f.className += ' sekt_generated';
jQuery(f).find('.sekt_generated');
 
pending…
jQuery(f).find('[sekt=generated]');
sekt_generated_f.setAttribute('sekt','generated');
jQuery(f).find('[sekt=generated]');
 
pending…
f.querySelector('[sekt=generated]');
sekt_generated_f.setAttribute('sekt','generated');
f.querySelector('[sekt=generated]');
 
pending…
f.querySelector('.sekt_generated');
sekt_generated_f.className += ' sekt_generated';
f.querySelector('.sekt_generated');
 
pending…
document.querySelector('[sekt=generated]');
sekt_generated.setAttribute('sekt','generated');
document.querySelector('[sekt=generated]');
 
pending…
document.querySelector('.sekt_generated');
sekt_generated.className += ' sekt_generated';
document.querySelector('.sekt_generated');
 
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