jQuery selector vs hasClass

JavaScript performance comparison

Revision 24 of this test case created by kp

Info

Test which is faster, a selector including the class compared to hasClass.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<html class="ios">
<body>
  <h1>Test</h1>
</body>
</html>

Preparation code output

Test

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
selector
if ($("html.ios").length > 0) {
 return true;
}
pending…
hasClass
if ($("html").hasClass("ios")) {
 return true;
}
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