All three questions: Yes. You can!
Neccessery: no
Better Performance: Possible
You can try and do a test. But the reason for caching is not looking for the whole DOM for your selector. Finding a document and a window is not a problem because they are two root variables. $ Caching (this) is situation-specific. See my second review.
Always cache the parent on which the queries are executed:
var header = $('#header'); var menu = header.find('.menu'); // or var menu = $('.menu', header);
Better to bind jQuery methods than cache selectors:
$('li.menu-item').click(function () {alert('test click');}) .css('display', 'block') .css('color', 'red') fadeTo(2, 0.7);
The cache items you frequently request are:
var header = $('#header'); var divs = header.find('div'); var forms = header.find('form');
Free Extra Tip:
Selectors are compressed to the slowest:
Id > Tag > classes
source share