hasClass-vs-is

JavaScript performance comparison

Revision 15 of this test case created by Max

Preparation code

<span id="first" class="class1 class2">Element</span>
<span id="second" class="class2">Second Element</span>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var firstElement = $('#first');
    var secondElement = $('#second');
};
</script>

Preparation code output

Element Second 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 (having both classes)
if( firstElement.hasClass('class1') || firstElement.hasClass('class2') ) {
  console.log("hasClass");
}
pending…
is (having both classes)
if( firstElement.is('.class1, .class2') ) {
  console.log("is");
}
pending…
hasClass (having second class only)
if( secondElement.hasClass('class1') || secondElement.hasClass('class2') ) {
  console.log("hasClass");
}
pending…
is (having second class only)
if( secondElement.is('.class1, .class2') ) {
  console.log("is");
}
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