0

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.)

Diego
  • 569
  • 1
  • 9
  • 28
  • Try to limit them both. Variables are less harmful then calling length all the times in a loop. So `var max = a.length; for (var i = 0; i < max; ++i)` is faster than `for (var i = 0; i < a.length; ++i)` . – Mouser Jan 26 '15 at 22:51
  • You're misunderstanding garbage collection. That probably won't help. (unless you're storing & dropping lots of object, in which case it still won't help) – SLaks Jan 26 '15 at 22:52
  • First you have to work out your criteria for determining one strategy over the other, otherwise your dilemma will have no end. In terms of performance, the difference between local and global variables is likely insignificant. If you want modular, maintainable code, then local variables are much preferred. Every time you call a function, a new execution context is created. Every time a function ends, that execution context is available for garbage collection (unless it persists as the result of a closure). – RobG Jan 26 '15 at 22:53
  • `those which I create with "var" and which die at the end of the function, because they become garbage` That isn't true; GC is about object, not variables. If they don't hold objects, they don't touch the GC; if they do, you need to be concerned about object lifetime. (holding it longer won't help you) – SLaks Jan 26 '15 at 22:59
  • Thanks guys for this correction. Seems that I was misled by wrong informations on some websites, they said that all variables may go to the garbage collector, even function arguments, even variables secretly created when we use .substring() or concatenation. But what happens to the memory alocated to these variables then ? Is it freed only when the page is closed ? – Diego Jan 31 '15 at 01:04

1 Answers1

2

Javascript has automatic garbage collection. While you don't want to get carried away with throwaway variables, using functions to properly scope your variables and manage your application will make this a non-issue in almost every case.

You should be using for(var i...) for all your iterative loops because, as you said, the other options are either 1) stupid long variable names or 2) namespace collisions.

In general you should use functions to namespace and modularize the components of your game/application. To avoid any real name space collisions you can wrap your entire application in a function to create a private namespace, like:

(function(){

   //code goes here

})();

I recommend you read Memory Management and this StackOverflow question on global variables.

Writing Fast, Memory-Efficient JavaScript By Addy Osmani is also a great article on Garbage Collection in Javascript.

Community
  • 1
  • 1
Aweary
  • 2,302
  • 17
  • 26