jquery is vs hasclass vs pure

JavaScript performance comparison

Revision 20 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];
 
  function hasClassRegex(el, selector) {
   var className = " " + selector + " ";
 
   if ((" " + el.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1) {
    return true;
   }
 
   return false;
  }

  function hasClassSplit(element, clz) {
var cn = element["className"] || "";
    var split = cn.split(" ");
    for(var i = 0;i < split.length;i++) {
      if(split[i] === clz) {
        return true;
      }
    }
}

  function hasClassIndexOf(element, clz) {
     var className = element.className;
     return className.indexOf(clz) !== -1;
  }
</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…
pureJsRegex
hasClassRegex(tElement, 'someClass');
pending…
pureJsSplit
hasClassSplit(tElement, 'someClass');
pending…
pureJsIndexOf
hasClassIndexOf(tElement, '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