jQuery selector vs hasClass (v14)

Revision 14 of this benchmark created on


Description

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

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<html>
<head></head>
<body class="marera"></body>
</html>

Test runner

Ready to run.

Testing in
TestOps/sec
selector
if (body.className.indexOf("foobar") >= 0) {
 console.log('yep');
}
ready
hasClass
if ($("body").hasClass("foobar")) {
 console.log('yep');
}
ready

Revisions

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