I created a jQuery plugin to modify my navigation. Unfortunately I have to access and modify almost all child elements such as <ul>, <li>, <a> and so forth. Those elements are then needed between one and four times.
Should I store them all in variables or should I access them like $('.my-nav').find('li') or $('.my-nav').find('li') when needed?
It just seems like a waste of memory to have 5 variables for maybe 25 lines of code.
But I don’t know if this is a acceptable trade-off for more performance.
I created a Fiddle to illustrate what is meant: http://jsfiddle.net/Yj35Q/2/
Always good practice to cache your nodes. You can also benchmark yourself using http://jsperf.com/
You don’t really need to worry about how much storage space variables need unless you are storing a massive DOM tree or something. Much more relevant the amount of work the JS engine has to do to locate the nodes.
EDIT
or even better, you can find an existing test case someone else has already created
http://jsperf.com/ns-jq-cached/4