1

I have a big object with a lot of key-value pairs (like a dictionary). When something happens I want to remove key-value from this object. I'm aware of delete performance issue and also setting unused key to undefined/null looks like a good way but I'm still worried about GC. I can set object to undefined or null but key (property) still standing there and holds some memory. When I delete key, it disappears so I guess that means GC will free the key's memory. So it's confusing for me to find the true solution to remove key from a big key-value collection object. Maybe some suggestions or references?

Example:

var dic = {};

dic.a = 1234;
dic.b = "hello";
dic.c = "!#$";

delete dic.a;         // { b: "hello", c: "!#$" }
dic.b = undefined;    // { b: undefined, c: "!#$" }
dic.c = null;         // { b: undefined, c: null }
Thus Spoke Nomad
  • 2,372
  • 4
  • 17
  • 34
  • Setting the property to an `undefined` or `null` value won't remove the property it just makes the property be that specific value. So now you are mixing up property definition checks with null/undefined checks. – MinusFour Apr 10 '17 at 01:12
  • @MinusFour Yeah, that's why I'm worried about GC. I want property to be garbage-collected and free'd. – Thus Spoke Nomad Apr 10 '17 at 01:14
  • What performance issues are you running into deleting properties? What memory issues are you running into when leaving properties? – Matt Apr 10 '17 at 02:24
  • @Matt `delete`ing property has performance issue, making it `undefined` has memory issue. When I use `delete` I can free property's memory but it's very costly, also making it `undefined` is a fast solution but property still holds some space in memory. – Thus Spoke Nomad Apr 10 '17 at 07:42
  • @MFatihMAR That's apparent from your question but what I was trying to get at was the issue that your program is actually running into. Are you running out of memory? Are deletes taking to long? There's no generic answer to this as it's a performance/memory trade off so I think you need to provide some scope around your issues/goals. How many millions of records are your dealing with? How many are you deleting? How long lived are the objects? How large are your keys etc. – Matt Apr 11 '17 at 02:01

1 Answers1

1

What about using a Map instead of a fat JS object ?

var dic = new Map()

dic.set("a", 1234);
dic.set("b", "hello");
dic.set("c", "!#$");

dic.delete("a");

Depending on your use case, you may also consider using Weak Maps

Community
  • 1
  • 1
Antoine Trouve
  • 1,198
  • 10
  • 21