I am trying to make my javascript games as light and smooth as possible, and here is my dilemma : What is worse, too many garbage or too many global variables ?
On one hand, to avoid micro pauses caused by garbage collection, I should avoid using temporary variables in my functions and for loops, those which I create with "var" and which die at the end of the function, because they become garbage.
But on the other hand, if I replace all these temporary variables with as many persistent global variables, won't it make my program heavier to run for the browser's javascript engine ?
What is worse ?
(Judging only on ease of writing and avoiding bugs, I would never get rid of temporary variables. For example, if a function A calls a function B and both have a for(i=...) instead of for(var i=...), function B's for loop will accidently mess up with function A's "i" since it would be the same global variable instead of two different temporary ones belonging to each function, and there will be bugs. The only way to avoid this with global variables is to use longer more explicit names, and that is annoying when you have to write them a lot. But garbage collection micro pauses are annoying in games so I must avoid temporary variables. What a dilemma.)