title: Avoid garbage collection in high performance JavaScript applications
body: <p>To achieve smooth animations in JavaScript, I have to avoid garbage collection. I was wondering, if the following "out-scoping" of the variable "key" would reduce garbage collection. The function "do" is called very often. In my example the variable "key" is kept alive after the function "do" terminates, but does this actually help?</p>
  
  <pre><code>define(function() {
  
      // keep reference to key to avoid garbage collection
      var key;
  
      function Test() {
          this.object = Object.create(null);
      }
  
      Test.prototype.do = function() {
          for (key in this.object) {
              this.object[key]++;
          }
      }
  
      return Test;
  
  });
  </code></pre>
  
  <p>Furthermore articles always talk about avoiding string and object creation to reduce garbage, do integers not produce garbage too or is this just negligible?</p>
  
  <p>EDIT: I think this is not a duplicate of <a href="https://stackoverflow.com/questions/18364175/best-practices-for-reducing-garbage-collector-activity-in-javascript">this question</a>, because I am asking specifically if it helps to out-scope a simple variable in a function and this has been asked, but not answered in the other question. Also the question about integers has not been answered.</p>
  
  <p>Background information: this is about particles, to animate particles, I require many calculations for 1000 objects. All the objects are being reused so no garbage problem exists there, but within the function, many variables are created on every call.</p>