jquery is vs hasclass vs pure (v9)

Revision 9 of this benchmark created on


Description

Added TestCase6, to also test pureJS in a "class not existent matter". Added variable "target" to ensure, the get()/getElementById process doesn't count towards the tests Added pureJS indexOf instead of regex.

Preparation HTML

<a class="someClass" id="someElement" href="#">link</a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var $t = $("#someElement");
  var target = $t.get(0);
  
  function hasClass(el, selector) {
   var className = " " + selector + " ";
  
   if ((" " + el.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1) {
    return true;
   }
  
   return false;
  }

function hasClass2(element, className) {
    return (' ' + element.className +  ' ').indexOf(' ' + className + ' ') > -1;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
is
$t.is(".someClass");
ready
hasClass
$t.hasClass("someClass");
ready
pureJs
hasClass(target, 'someClass');
ready
is non existing
$t.is(".otherClass");
ready
hasClass non existing
$t.hasClass("otherClass");
ready
pureJs non existing
hasClass(target, 'otherClass');
ready
pureJs hasClass2
hasClass2(target, 'someClass')
ready
pureJs hasClass2 non-existing
hasClass2(target, 'otherClass')
ready

Revisions

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