Reputation: 586
I've seen people use code that looks like
const STUFF_MAP = Object.create(null);
STUFF_MAP
would like suggested only be used like a map, so I understand why using Object.create(null)
is better than {}
(removing the properties that could cause conflicts etc.), but is there any advantage to not just use
const STUFF_MAP = new Map();
When would I use each one? Does it have to do with being compiled to pre es6?
Upvotes: 2
Views: 459
Reputation: 664650
Does it have to do with being compiled to pre es6?
Yes, Object.create(null)
is ES5 and won't need a polyfill for Map
, if you need to support old browsers.
Apart from that, you should prefer the more modern Map
which is also clearly indicates the purpose of a variable. You will need to access the collection using has
/get
/set
methods, not in
operator/bracket syntax/assignment. This can sometimes lead to code that isn't as concise, e.g. for incrementing a value you cannot use a compound assignment operator.
As for performance, there should not be much of a difference; if you care, do a benchmark for your particular situation.
Upvotes: 2