True Way To Remove Property From Object (delete vs null vs undefined)

时间:2017-04-10 01:05:54

标签: javascript node.js performance memory

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 }

1 个答案:

答案 0 :(得分: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