Reputation: 4823
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.
Upvotes: 2
Views: 636
Reputation: 887365
Actually, using delete
still relies on garbage collection; it just makes more work for the GC.
Calling delete x.y
doesn't free any memory; it just clears x
's reference to y
.
If y
has 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)
Upvotes: 4