[wp-trac] [WordPress Trac] #15650: Inefficient selectors in common.dev.js
WordPress Trac
wp-trac at lists.automattic.com
Sat Dec 11 19:54:36 UTC 2010
#15650: Inefficient selectors in common.dev.js
-------------------------+--------------------------------------------------
Reporter: GamajoTech | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Future Release
Component: Performance | Version:
Severity: minor | Keywords: needs-refresh
-------------------------+--------------------------------------------------
Comment(by GamajoTech):
Replying to [comment:2 azaozz]:
> These two do the same thing:
> {{{
> $('.wp-menu-toggle', menu)...
> }}}
>
> {{{
> menu.find('.wp-menu-toggle')...
> }}}
>
> They limit the search for the `.wp-menu-toggle` class only to children
of `menu`.
Agreed.
>Did some testing few months ago and the first one was slightly faster.
As the first is converted internally to use the second, I find your
results interesting. Could you share your test code?
From http://api.jquery.com/jQuery/
Internally, selector context is implemented with the .find() method, so
$('span', this) is equivalent to $(this).find('span').
I see no reason not to jump straight to using the find() method for the
optimizations I've listed.
> It can also be written as:
> {{{
> $('li.wp-has-submenu', '#adminmenu')...
> }}}
> i.e. find `li` nodes that have `wp-has-submenu` class but search only in
the node with id = `adminmenu`.
Actually, no it can't. Again from http://api.jquery.com/jQuery/ the
context is:
A DOM Element, Document, or jQuery to use as context
A string doesn't cut it; the context would still be "document", and not
"#adminmenu".
Further information at http://brandonaaron.net/blog/2009/06/24
/understanding-the-context-in-jquery
--
Ticket URL: <http://core.trac.wordpress.org/ticket/15650#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list