jQuery class vs attribute selectors

Benchmark created by Benjamin Bostow on


Description

Test performance of jQuery of using the class selector ".classname" vs the attribute selector "[class='classname']"

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="content">
  <div class="sub-content">
    Lorem ipsum dolor sit amet.
  </div>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
class selector
var content = jQuery('div.content');
ready
attribute selector
var content = jQuery('div[class="content"]');
ready
attribute contains selector
var content = jQuery('div[class*="content"]');
ready
attribute contains word
var content = jQuery('div[class~="content"]');
ready
attribute not equal selector
var content = jQuery('div[class!="content"]');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.