hasClass vs match (v28)

Revision 28 of this benchmark created by Erz on


Preparation HTML

<span class="irrelevant class4 irrelevant2">Element</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var element = $('span');

Test runner

Ready to run.

Testing in
TestOps/sec
hasClass - found
if (element.hasClass('class4')) {
  console.log("hasClass");
}
ready
match - found
if(element.attr('class').match(/\bclass4\b/) ) {
  console.log("match");
}
ready
hasClass - not found
if (element.hasClass('class2')) {
  console.log("hasClass");
}
ready
match - not found
if(element.attr('class').match(/\bclass2\b/) ) {
  console.log("match");
}
ready

Revisions

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