Here is the object and global namespace for my Javascript code.
Users.Cache.data = { x:'1', y:'2', z:'3'};
if I want to clear all the entries in data, I have 2 ways :
1) Users.Cache.data = {};
// Garbage Collection takes care of it for removing from memory.
2) for(i in Users.Cache.data){
delete(Users.Cache.data[i];
}
Anyone can comment if any specific approach is better: consider these as criteria:
a) Memory leak ( looks like neither has a problem of memory leak)
b) Performance.
Actually, using
deletestill relies on garbage collection; it just makes more work for the GC.Calling
delete x.ydoesn’t free any memory; it just clearsx‘s reference toy.If
yhas no other references, it will become unrooted and open to eventual garbage collection.Your second option will end up creating more unrooted references for the GC to go through.
(Note that I haven’t actually measured anything)