Element + ID vs only ID in jquery?

前端 未结 3 456
情歌与酒
情歌与酒 2021-01-17 07:14

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 fast

3条回答
  •  孤独总比滥情好
    2021-01-17 07:27

    See Optimize Selectors:

    Beginning your selector with an ID is always best.

    and

    ID-only selections are handled using document.getElementById(), which is extremely fast because it is native to the browser.

    So the answer is: $('#some_id') should be faster.

提交回复
热议问题