hasClass (v10)

Revision 10 of this benchmark created on


Description

This will test a number of different alternatives for a hasClass function

Preparation HTML

<div id="test" class="a">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://github.com/dgalarza/RaptorJS/raw/master/src/raptor.pack.js">
</script>
<script>
  // RaptorJS
  var $p = raptor.pack; // to keep prop look-ups equal with competition
  var raptor_test = document.getElementById('test');
  var jQuery_test = $('#test');

  Element.prototype.matchesSelector = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
another hasclass
function hasClass(item, cname) {
  cname = cname.toLowerCase();
  var classes = item.className.toLowerCase();
  return (classes.indexOf(cname) != -1 && cname.length == classes.length) || (classes.indexOf(" " + cname) != -1) || (classes.indexOf(cname + " ") != -1);
}

hasClass(raptor_test, "a");
ready
jQuery
jQuery_test.hasClass('a');
ready
jQuery Cached
$(raptor_test).hasClass('a');
ready
String search
function hasClass(element, className) {
  return (" " + element.className + " ").indexOf(" " + className + " ") != -1;
}

hasClass(raptor_test, "a");
ready
matchesSelector
raptor_test.matchesSelector(".a");
ready

Revisions

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

  • Revision 1: published by bbarr on
  • Revision 2: published by dgalarza on
  • Revision 3: published by Bruno Garcia on
  • Revision 4: published by Bruno Garcia on
  • Revision 10: published on