javascript - When should I use $(object), and when should I use $object? -


this question has answer here:

suppose have element matching ".foo".

<div class="foo"></div> 

i've learned experience performance hit calling finders more once, in, if i'm trying change same object several times.

$(".foo").doonething(); $(".foo").doanotherthing(); $(".foo").dosomethingelse(); // makes jquery ".foo" 3 times, bad! 

versus

$foo = $(".foo"); $foo.callallthreeofthethingmethodsinthepreviousblock(); // calls $(".foo") once, occupies memory jquery object version of foo div. 

my question is, how many times have use finder before setting aside memory hold jquery object instead of making jquery method call again?

i ask since other day had made every finder called more once stored $object variable; boss had said instead use finder rather stored object if using finder, say, 2 or 3 times. what's take on this?

use var $foo = $(".foo");

using $(".foo") multiple time make jquery repeat search. looks jquery doesn't cache selectors, , if did better smart , not rely on else.

does jquery kind of caching of “selectors”?


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -