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?
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;
});
Furthermore articles always talk about avoiding string and object creation to reduce garbage, do integers not produce garbage too or is this just negligible?
EDIT: I think this is not a duplicate of this question, 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.
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.