For example, will the first piece of code perform a full search twice, or is it smart enough to cache results if no DOM changes have occurred?
if ($(\"#navba
It's not so much a matter of 'does it?', but 'can it?', and no, it can't - you may have added additional matching elements to the DOM since the query was last run. This would make the cached result stale, and jQuery would have no (sensible) way to tell other than running the query again.
For example:
$('#someid .someclass').show();
$('#someid').append('New!');
$('#someid .someclass').hide();
In this example, the newly added element would not be hidden if there was any caching of the query - it would hide only the elements that were revealed earlier.