hasClass-vs-is

JavaScript performance comparison

Revision 22 of this test case created

Info

adding more classes to find out if there's a tipping point when number of classes becomes more efficient with ".is()" - adding match() test also

Preparation code

<span class="irrelevant class4 irrelevant2">Element</span>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var element = $('span');
};
</script>

Preparation code output

Element

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
hasClass
if (element.hasClass('class1') || element.hasClass('class2') || element.hasClass('class3') || element.hasClass('class4') || element.hasClass('class5')) {
  console.log("hasClass");
}
pending…
is
if (element.is('.class1, .class2, .class3, .class4, .class5')) {
  console.log("is");
}
pending…
match
if( element.attr('class') !== undefined && element.attr('class').match(/class1|class2|class3|class4|class5/) ) {
  console.log("match");
}
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