What you wrote would work, and it would be faster than the original code,
but you're making an unnecessary $() call each time you use $(list).
Instead, do it like this:

var $list = $("ul.list");

$list.find("li.selected").addClass("blah");

The $ in $list doesn't have any special meaning; it's just a naming
convention to help remember that $list is already a jQuery object and
doesn't need $() when used.

-Mike

> From: Darko Romanov
> Sent: Tuesday, July 07, 2009 8:56 AM
> To: jQuery (English)
> Subject: [jQuery] Caching nodes
> 
> 
> Hi there,
> 
> I'm developing a site that has some js visual effects and 
> many graphic elements to move. So I have to take care of each 
> single query on the DOM as it could take too much time to be 
> executed, resulting in a flicking animation.
> 
> I thought I could cache in memory a node and then querying 
> it, but I can't understand if it made much sense. Something like:
> 
> var list = $("ul.list");
> 
> and then executing any query in ul.list in this way:
> 
> $(list).find("li.selected").addClass("blah");
> 
> instead of:
> 
> $("ul.list li.selected").addClass("blah");
> 
> would it be faster?
> 
> tnx
> 

Reply via email to