I have a somewhat related observation.  I have discovered that when selecting by ID, the context parameter does not matter.  In other words, these two statements are functionally equivalent:

$("#myId");
and
$("#myId", myContext);

It's as if when the id selector is passed then the $ method just uses a document.getElementById.  Besides the fact that this may produce unexpected results, it also means that you should be careful when using IDs to try to gain performance.  Because if the size of your markup is large then this

$("#myId", myContext);

may actually be slower then this

$(".myClass", myContext);

I have definitely seen performance gains by using the second one over the first.

Brendan

On 10/9/06, Raziel Alvarez <[EMAIL PROTECTED]> wrote:
Thanks for all your responses. I actually do all the things that you mentioned: reusing the jQ object, chaining, setting a context, etc. Actually it would be helpful to have some performance analysis on the different kinds of queries, such as searching by ID, by element, class name, attribute, xpath queries, context-delimited, etc., because even though it's somewhat straightforward to know that a search by ID is faster than searching by class name, it'd be interesting (and useful) to know by how much.


 
On 10/7/06, Matt Stith <[EMAIL PROTECTED] > wrote:
yes, that would help quite a bit, since jquery would only need to look once.

On 10/7/06, Jacky < [EMAIL PROTECTED]> wrote:
> Would caching the jQuery object a help too? Sometimes you just can't
> keep the chain.
>
> e.g. var jqObj = $("#abc");
>
> On 10/8/06, Karl Swedberg <[EMAIL PROTECTED]> wrote:
> > On Oct 7, 2006, at 3:39 PM, George Adamson wrote:
> >
> > > An easy performance booster is to use the second param in $() to set a
> > > context for the search. Eg: $("DIV.myClass", myParentElement).
> > > Perhaps this
> > > is what you meant when you mentioned 'getting a parent element' ?
> > >
> > > Chaining methods is helpful so you can avoid re-querying. If you
> > > need to put
> > > other code in betwen method calls then reusing the same JQuery
> > > object by
> > > putting it into a variable beforehand is worth while to save
> > > requerying.
> > >
> > > If you're going to do several queries inside the same parent element
> > > (s) then
> > > a combination of the above will be a big help.
> >
> > Those sound like good suggestions to me, though I'm no expert.
> >
> > Something I try to keep in mind is the relative speed of different
> > types of queries. This has been mentioned on the list before, but in
> > case you didn't see it, references to IDs are fastest, followed by
> > elements, and then classes. At least, that's how I've understood
> > previous discussions of the topic. So:
> > a.  $('#my-id') is faster than $('div#my-id'), and
> > b. $('div.my-class') is faster than $('.my-class')
> >
> > Hop that helps.
> >
> > Karl
> > _______________________
> > Karl Swedberg
> > www.englishrules.com
> > www.learningjquery.com
> >
> >
> > _______________________________________________
> > jQuery mailing list
> > discuss@jquery.com
> > http://jquery.com/discuss/
> >
>
>
> --
> Best Regards,
> Jacky
> 網絡暴民 http://jacky.seezone.net
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to