jquery is vs hasclass vs pure

JavaScript performance comparison

Revision 14 of this test case created

Preparation code

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

  function hasClassArray(el, selector) {
    var classArray = el.className.split(' ');
    return classArray.indexOf(selector) >= 0;
  }

  function hasClassForLoop(el, selector) {
    var lst = el.className.split(' ');
        for (var i = 0, n = lst.length; i < n; i++)
        {
                if (lst[i] == selector) 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("a.someClass");
pending…
hasClass
$t.hasClass("someClass");
pending…
pureJs
hasClass($t.get(0), 'someClass');
pending…
pureJs using array
hasClassArray($t.get(0), 'someClass');
pending…
foor loop
hasClassForLoop($t.get(0), '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