<p>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?</p>

<pre><code>if ($("#navbar .heading").text() &gt; "") {
  $("#navbar .heading").hide();
}
</code></pre>

<p>and </p>

<pre><code>var $heading = $("#navbar .heading");

if ($heading.text() &gt; "") {
  $heading.hide();
}
</code></pre>

<p>If the selector is more complex I can imagine it's a non-trivial hit.</p>

i don't believe jquery does any caching of selectors, instead relying on xpath/javascript underneath to handle that. that being said, there are a number of optimizations you can utilize in your selectors. here are a few articles that cover some basics:
Optimizing jQuery selector performance
Performance analysis of selectors