tags:

views:

237

answers:

7

looking into selector performance..

+2  A: 

Just as a guess, I'd say selecting by ID is faster, since document.getElementByID() is a built-in function to javascript, while 'getElementByClass()' is something that various custom functions have been built to solve. Ideally too, looking for $('div#id') is going to be faster because it's more specific and allows the search loop to cut out certain paths that have no chance of coming back positive.

matt lohkamp
have you done any research to back the `div#id` claim? i don't see that there's any native implementation to match that in a way that the specificity of the selector could be utilized.
David Hedlund
+3  A: 

In general, searching for id's is done by getElementbyId, which is the fastest possible way to select a DOM element. If available, getElementByClass is used to grab a node by class name.

Again, getElementById is the fastest way. Performing getElementById three times against one getElementByClass needs some benchmarking to find out the speed difference.

But if the browser does not support getElementByClass, it's even more slow.

Kind Regards

--Andy

jAndy
+4  A: 

Use this instead, if you want performance:

$("#id1").add("#id2").add("#id3")

There's less string parsing to do here. That should be faster than selecting by class name, unless the browser has a native implementation (some do).

Eric
A: 

jQuery selectors work on same principle as that of CSS selectors(generalized statement not an exact fact).And in CSS , id selector are more faster when compared to that of class selector.So $("#myDiv") is more faster than #(".myDivClass").

Pawan Mishra
+3  A: 

Looks like .class is working faster for this case. jQuery might not be going the getElementById route. Chrome and Safari are probably being optimizing with getElementsByClassName.

Tests @ http://jsfiddle.net/mGqyH/4/

Chrome

Chrome


Safari

alt text


Firefox

Firefox performance


Document used (modified)

http://www.w3.org/TR/DOM-Level-2-Events/events.html

combined IDs selector

$("#Events, #table-of-contents, #Events-overview, #Events-flow-capture, #Events-EventTarget, #Events-EventListener")

disjoint IDs selector

$("#Events").add("#table-of-contents").add("#Events-overview").add("#Events-flow-capture").add("#Events-EventTarget").add("#Events-EventListener");

class selector

$(".selectMe")
Anurag
What about in IE?
Eric
I don't have access to IE. but made the answer a CW. Feel free to add results foe IE here.
Anurag
Interesting how firefox's time for disjoint ID's is so large.
Eric
Got results similar to Firefox on Opera too. The query goes through `jQuery(document).find("#someID")` for each `add` call, which in turn uses the new Sizzle engine. But since it works fast on Webkit browsers, it might have to do with tweaks and optimizations by the browser itself.
Anurag
+2  A: 

Updated http://jsfiddle.net/uD7Qz/1/ with .add method.

.add method is almost as fast as $(.class), on Chrome. On FireFox it's 4x slower then $("#1, #2, #4").

Atleast those were my results.

Peeter
Interesting results. I got the same for Chrome and Safari (`.add` and `.class` both very close), but in Firefox (`.add` took 5x the time whereas `#a, #b, #c` and `.class` were both very close.
Anurag