jquery is vs hasclass vs pure

JavaScript performance comparison

Revision 10 of this test case created

Preparation code

<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>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
is
$t.is(".someClass, .foo, .foo2");
pending…
hasClass
$t.hasClass(".someClass, .foo, .foo2");
pending…
pureJs
hasClass(tElement, 'someClass');
pending…
pureJs2
$t.hasClassFast('someClass');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment