jQuery .hasClass() vs .is() vs native performance (v8)

Revision 8 of this benchmark created by Ross on


Description

Compare hasClass() to is() when checking for class attribute

Preparation HTML

<input type="checkbox" id="foo" class="bar" checked="checked"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var $foo = $('#foo');
  var _foo = document.getElementById('foo');
var matches = function(el, selector) {
  return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
};

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
.hasClass()
$foo.hasClass('bar');
ready
.is()
$foo.is(".bar");
ready
native
matches(_foo, '.bar');
ready

Revisions

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