99

is there a preferred method of determining whether an element is assigned a class, from a performance standpoint?

$('#foo').hasClass('bar');

or

$('#foo').is('.bar');
Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
  • 6
    You can always validate performance on this site as well over multiple browsers: http://jsperf.com ...These are some test cases to browse: http://jsperf.com/browse – iwasrobbed Feb 04 '11 at 18:35

4 Answers4

166

Update:

I committed a test following a comment and four upvotes to very comment. It turns out that what I had said is the correct answer. Here is the result:

enter image description here

http://jsperf.com/hasclass-vs-is-so


The is is multi-purpose, you can for example do is('.class'), is(':checked'), etc which means is has more to do where is hasClass is limited which only checks for a class being set or not.

Hence, hasClass should be faster if performance at any level is your priority.

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 7
    Do you have proof that it is faster? Just because a function has less funtionality does NOT mean it's automatically faster. – Kris Feb 04 '11 at 19:54
  • 16
    @Kris: Yes I do: http://jsperf.com/hasclass-vs-is-so. As I said, `hasClass` ***is*** a lot more faster. – Sarfraz Feb 24 '12 at 17:17
  • 5
    'a lot more faster' => 'much faster' – Kirk Strobeck Apr 09 '12 at 15:29
  • @SgtPooki, I was simply asking for proof; if it was true I wanted to know why. I am inquisitive like that :). You can easily have more functionality and have it be faster; it always depends on implementation. Your 99% is a sweeping generalization and not necessarily the true state of software development. Your question of "Will jQuery ever be as fast as straight JavaScript? An answer other than no is wrong." is not a good question; jQuery actually has many optimizations in which will provide faster access to DOM elements once data is cached; it really depends on your specific usage. – Kris Mar 27 '13 at 17:34
  • @SgtPooki "Additional CPU cycles" would mean slower every time I agree but I'm not sure why we're talking about your interpretation. I asked for an explanation because, as I already mentioned, less functionality does not mean faster. I am not sure of your intent to state "Software developers seem to be forgetting all about CPU cycles"; it appears you are stating, indirectly, that I am forgetting about CPU cycles but I am purporting the opposite; additional functionality may not mean additional CPU cycles as it depends on implementation. It always depends on implementation. – Kris Mar 28 '13 at 06:14
  • 1
    @SgtPooki as far as faster take a look at the sizzle selector engine; as I continue to mention it depends on implementation and in older browsers that do not natively support queries it will most likely be faster through jQuery's implementation as there is no native version (win by default). Also if you're continuing to recall the same DOM elements, depending on browser and version, it may take additional time where as jQuery can cache this element for faster retrieval. Typically native JavaScript functions will be faster; but it depends on browser support and implementation. – Kris Mar 28 '13 at 06:17
14

Since is is more generic than hasClass and uses filter to process the provided expression, it seems likely that hasClass is faster.

I ran the following code from the Firebug console:

function usingIs() {
for (var i=0; i<10000;i++) {
 $('div#example-0').is('.test');
}
}

function usingHas(){
for (var i=0; i<10000;i++) {
 $('div#example-0').hasClass('test');
}
}

usingIs();
usingHas();

I got:

  • usingIs: 3191.663ms
  • usingHas: 2362.523ms

Not a huge difference, but may be relevant if you're doing lots of testing.

EDIT when I say 'not a huge difference, my point is that you need to do 10000 cycles in order to see 0.8s of a difference. I'd be surprised to see a web application such that switching from is to hasClass would see a significant improvement in over all performance. However, I grant that this is a 35% improvement in speed.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
9

Both should be pretty close in terms of performance but $('#foo').hasClass('bar'); seems more readable and semantically correct to me.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
7

.hasClass is much faster than .is You can test it from here. Change the test case according to you.

user113716
  • 318,772
  • 63
  • 451
  • 440
Gaurav
  • 28,447
  • 8
  • 50
  • 80