jquery is vs hasclass vs pure (v10)

Revision 10 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<a class="someClass" id="someElement" href="#">link</a>


<script>
  var $t = $("#someElement"), tElement = $t[0];

$.fn.hasClassFast = function (testClass) { for (var i = 0, j = this.length; i < j; ++i) { if ((' ' + this[i].className + ' ').indexOf(' ' + testClass + ' ') === -1) { return false; } } return true; };

  function hasClass(el, selector) {
   var className = " " + selector + " ";
   if ((" " + el.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1) {
    return true;
   }
  
   return false;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
is
$t.is(".someClass, .foo, .foo2");
ready
hasClass
$t.hasClass(".someClass, .foo, .foo2");
ready
pureJs
hasClass(tElement, 'someClass');
ready
pureJs2
$t.hasClassFast('someClass');
ready

Revisions

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