jQuery selectors using id attribute

May 15, 2012 at 1:42 PM

In the 'Microsoft Project Silk.pdf' file, the following is mentioned:

"this approach often causes jQuery to search the entire DOM to find the element, which can have significant impact on the client-side performance of your application."

In what way is this the case? I always thought/learned that using the id in a jQuery selector was the fastest and most efficient because it can use the browser-method 'document.getElementById'. Other selectors force jQuery to search all or part of the DOM, or that's what I thought.

I can agree the data- selectors may be a better idea, especially when the id's are determined by designers, but I believe the PDF isn't entirely correct on that point.

Other than that, the PDF is a great read and very educational. Thanks for that.

May 15, 2012 at 8:55 PM


I also agree that the use of the ID selector is the fastest selector in jQuery. As it directly uses the native getElementById() JavaScript method, which is a very fast method to access an element in the document with an specific id. Although this might not be valid when having several elements with the same id.

Therefore, I believe the note you mentioned is mostly related to the other selection methods.

If you think this should be modified in the Silk's documentation you could create a work item in the Issue Tracker section so the p&p team analyzes your suggestion for future releases.


Agustin Adami