jQuery Selectors, efficiency

前端 未结 6 707
既然无缘
既然无缘 2020-12-08 23:16

I have been reading more lately about the efficiency of the different selector engines. I know that jQuery uses the Sizzle engine and this blog post about some jQuery stuff

6条回答
  •  萌比男神i
    2020-12-08 23:57

    You can see from the source code here: http://code.jquery.com/jquery-1.6.2.js in the function init.

    The fastest selector is $('') which just returns an empty jQuery object immediately.

    $('body') is next, which jQuery converts to document.body

    The next is $('#id') which uses document.getElementById('id').

    Any other selector such as $('div#id') will just become a call to $(document).find('div#id'). find() is very complex compared to any of those other solutions and uses Sizzle JS to select the div.

提交回复
热议问题