I know that Javascript has a garbage collector. Therefor, using delete remove only a reference to the object, and when there is no more reference to this object, it is deleted by the GC.
Javascript is tricky, with the closures, the fuzzy name space and the prototype inheritance, it's not always obvious to know when to now or why.
I am coding a fairly large Javascript project and would like to avoid memory leak, while limiting the global memory usage. I am not at all in the optimizing stage (let's get the stuff work first :-)), but it would be nice to know the good practices for memory management in order to avoid writing crappy code.
- So when should I use delete ?
- What are the traps I should avoid, using objects ?
- Some stuff to know about closures ?
- Some good practices to highlight ?
I think by storing data on DOM nodes you can easily create circular references which not all browsers can deal with. For example:
From my experience, Garbage Collectors are well/poorly implemented depending on the browser. Applying good Object Oriented programming practices is a good start.
My only advice: do not create memory leaks by connecting DOM & javascript objects (circular references that won't be cleared by DOM and JS GCs). These mistakes will eat far more memory than any object you will instantiate within your application.
More details on DOM/JS memory leaks. http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx