hasClass-vs-is (v34)

Revision 34 of this benchmark created by yaosi 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');
  var el = document.querySelector('span');

Test runner

Ready to run.

Testing in
TestOps/sec
hasClass
if (element.hasClass('class4')) {
  console.log("hasClass");
}
ready
is
if (element.is('.class4')) {
  console.log("is");
}
ready
match
if(element.attr('class').match(/class4/) ) {
  console.log("match");
}
ready
nat0
if ( el.getAttribute('class').match(/class4/) ) {
  console.log("className");
}
ready
nat1
if ( el.className.match(/class4/) ) {
  console.log("className");
}
ready
nat2
if ( element[0].className.match(/class4/) ) {
  console.log("className");
}
ready
nat4
if ( element[0].getAttribute('class').match(/class4/) ) {
  console.log("className");
}
ready

Revisions

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