title: Is there an impact of the jQuery function on Garbage Collection?
body: <p>This question is about how the jQuery function holds HTML elements, and whether there may be an impact on garbage collection.</p>
  
  <p>Consider the two following functions, that show or hide a certain &lt;div&gt; element in an HTML page.</p>
  
  <pre><code>function showError() { $("div#error").show(); }
  function hideError() { $("div#error").hide(); }
  </code></pre>
  
  <p>These two functions are always invoking jQuery to find the &lt;div&gt; element in the DOM tree; they could be refactored to eliminate the overhead of the jQuery function once, as such:</p>
  
  <pre><code>var divError = $("div#error");
  function showError() { divError.show(); }
  function hideError() { divError.hide(); }
  </code></pre>
  
  <p>But in this case, the <code>divError</code> variable is loosened in the global scope; we could pull some trick with a function:</p>
  
  <pre><code>var showError, hideError;
  (function() {
    var divError = $("div#error");
    showError = function() { divError.show(); }
    hideError = function() { divError.hide(); }
  })();
  </code></pre>
  
  <p>In all these cases, what is the impact on garbage collection ? Is it a problem to have the result of the call to the jQuery function floating in the global scope ?</p>
  
  <p>Is it a good discipline to try and minimize the call to the jQuery function ? Should these examples be followed or shunned ?</p>
  
  <p>If I replaced jQuery in these examples with <code>document.getElementById()</code>, to retrieve the HTML element, do the same concerns apply ?</p>