Search code examples
jqueryhtmlperformancedomhtmlelements

Element + ID vs only ID in jquery?


div#some_id will scan through all the divs throughout the DOM.

#some_id will pick up the ID directly from the DOM.

So which is faster? $('div#some_id') or $('#some_id')?


Solution

  • As ID are supposed to be unique in DOM, so div#some_id will be doing unnecessary scan on all DOM elements and #some_id will do a direct scan on it.

    You can also see the result here: div-some-id-vs-some-id