is-classname vs. hasClass

Benchmark created by Krinkle on


Description

$.fn.is is often said to be slow. However, how slow is it actually, or is it faster than alternatives ?

Here we compare hasClass to is('.someclass')

Preparation HTML

<div id="foobar" class="foo bar">Lorem ipsum</div>
<div id="lipsum" class="lorem ipsum">Foo bar</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.fn.is() (true)
$("#foobar").is('.bar');
ready
jQuery.fn.is() (false)
$("#lipsum").is('.foo');
ready
jQuery.fn.hasClass() (true)
$("#foobar").hasClass('bar');
ready
jQuery.fn.hasClass() (false)
$("#lipsum").hasClass('foo');
ready

Revisions

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

  • Revision 1: published by Krinkle on