1

I often use .class to select a single html element. However, this is not correct. I've read somewhere that using #id is the optimal approach when using jQuery. How much faster is using #id instead of .class when trying to select a single element?

plr108
  • 1,201
  • 11
  • 16
  • 2
    check http://stackoverflow.com/questions/6460644/in-jquery-is-selecting-by-class-or-id-faster-than-selecting-by-some-other-attri – nunoarruda Jan 30 '15 at 00:43
  • 2
    That's completely browser implementation dependent. – c-smile Jan 30 '15 at 00:43
  • 2
    I would think the performance is negligible but Id is probably faster because once it would find it, it would stop looking. I think its better to just use Id's when you want a single element, classes when you need multiple elements because it makes nicer code. – mattfetz Jan 30 '15 at 00:53

1 Answers1

4

According to this link that taken from ewwink's answer, yes selecting by ID are faster than single class in any common browser.

jsperf

Community
  • 1
  • 1
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 2
    This would make sense because instead of looking for every instance like you would with a class you would be looking for for a single element and then stopping. – mattfetz Jan 30 '15 at 00:56