hasClass vs css so

JavaScript performance comparison

Revision 4 of this test case created by fearphage

Info

To confirm a question asked here:

http://stackoverflow.com/questions/4901553/jquery-hasclass-vs-is

I belive hasClass should be faster.

Preparation code

<div id="foo">
  I am a DIV
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    $foo = $('#foo');
};
</script>

Preparation code output

I am a DIV

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 - OR
$foo.hasClass('bar') || $foo.hasClass('catpants');
pending…
is - OR
$foo.is('.bar,.catpants')
pending…
hasClass - AND
$foo.hasClass('bar') && $foo.hasClass('catpants');
pending…
is - AND
$foo.is('.bar.catpants')
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