hasClass-vs-is (v20)

Revision 20 of this benchmark created by HBender on


Preparation HTML

<span class="class1">Element</span>
<span class="class2">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
if( element.hasClass('class1') || element.hasClass('class2') || element.hasClass('class3') || element.hasClass('class4') || element.hasClass('class5') ) {
}
ready
is
if( element.is('.class1, .class2, .class3, .class4, .class5') ) {
}
ready
match
if( element.attr('class').match(/(^[3]{1}$|^[6]{1}$|^[7]{1}$|^[8]{1}$|^[1]{1}[4]{1}$|)/gm) ) {
}
ready
match + word boundaries
if( element.attr('class').match(/\bclass(^[3]{1}$|^[6]{1}$|^[7]{1}$|^[8]{1}$|^[1]{1}[4]{1}$|)\b/gm) ) {}
ready

Revisions

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