> So our points of optimization should be on plain tag selectors,
> and expressions that return no elements.
> -- Yehuda
Let's not get too carried away based on this single benchmark. It is only
checking a few things and I am not convinced they representative of
bottlenecks in real code. For example, I don't tend to set and remove a lot
of event handlers in a loop or other time-critical regions of code. I don't
think I use a lot of selectors that return no elements.
Also, you'd want to look at the dynamic counts of calls to $(), not the
static ones. If a selector is only used once in a ready handler, it's not
nearly as important to performance as one commonly used in an each loop or
keydown handler. Looking at my own code and guessing the dynamic counts of
arguments passed to $(), I would say that $(DOMElement)--as in $(this)--is
the most common followed by a mix of assorted selectors and then $("#id").
If ids are common, it might be productive to add a special case to
jQuery.find() for it, and you could do the same for tags--but watch out for
the context!
I'm not sure where the "jQuery is SLOW" comment comes from in the original
article. The only jQuery sin I see is that it is making hard things look
easy. A user can write $("#mydiv") versus $(".myclass") and not see the
performance issues that can crop up with the latter. Perhaps a "jQuery
profiler" could help by noting the number of nodes that were selected versus
the number of nodes traversed. If that ratio gets too low, the user should
try to restrict the context or use some other selector.
Here's another "hard things easy" example. The rounded corners plugin
generates and inserts two DOM elements for every pixel of rounding it has to
do on a top or bottom corner. Consider this deceptively small line of code:
$(".round").corner("20px"). If you specify that on a page that has 10 .round
elements, it looks through the entire DOM tree to find the .round elements,
then it generates and appends 800 elements (10 divs * 2 horizontal surfaces
per div * 20 pixel radius * 2 elements per pixel) to do the rounding! In
subsequent trips through the DOM, these rounding elements will also be
processed. So if you go crazy rounding with a large radius it can affect
performance. (Sounds like another job for the "jQuery profiler" I think.)
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/