hasClass vs is vs array.indexOf

JavaScript performance comparison

Revision 10 of this test case created

Info

To confirm a question asked here:

http://stackoverflow.com/questions/4901553/jquery-hasclass-vs-is

I belive hasClass should be faster.

Preparation code

<div id="foo">
  I am a DIV
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Preparation code output

I am a DIV

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
hasClass
$('#foo').hasClass('bar');
pending…
is
$('#foo').is('.bar');
pending…
array indexOf
document.querySelector('#foo').className.split(' ').indexOf('bar') >= 0
pending…
className indexOf
document.getElementById('foo').className.indexOf('bar')>=0
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