2

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?

Scrapper142
  • 566
  • 1
  • 3
  • 12
  • 3
    I think that code probably predates the `Map` type. – Barmar Jan 05 '22 at 19:49
  • 1
    There's very little reason to use `Object.create(null)`, just use `{}` – Barmar Jan 05 '22 at 19:50
  • Should I update to use a map? Also would doing so make it worse after compiling to older js? – Scrapper142 Jan 05 '22 at 19:50
  • 2
    Maps are more flexible about what you can use as the keys, but otherwise they're not much different from using ordinary objects. – Barmar Jan 05 '22 at 19:51
  • 2
    But you access them in different ways, so they're not interchangeable. – Barmar Jan 05 '22 at 19:52
  • Any performance benefit to a map if the keys are just strings anyway? – Scrapper142 Jan 05 '22 at 19:52
  • 1
    I doubt it, but you'd have to benchmark it. – Barmar Jan 05 '22 at 19:53
  • 1
    "*Any performance benefit to a map if the keys are just strings anyway?*" [Which is faster?](https://ericlippert.com/2012/12/17/performance-rant/) – VLAZ Jan 05 '22 at 19:53
  • 1
    @Barmar No, don't use `{}`, which inherits all kinds of unwanted stuff from `Object.prototype` and is not suitable as a map. – Bergi Jan 05 '22 at 21:33
  • 1
    @Bergi There are only a handful of properties in `Object.prototype` -- in ES6 they wised up and defined new methods in `Object` itself (e.g. `Object.entries()`). – Barmar Jan 05 '22 at 22:14
  • 1
    @Barmar Yes, but those are properties that you don't want as keys in your collection. – Bergi Jan 05 '22 at 22:51
  • 1
    @Bergi The chance that you'll have them as actual keys is negligible. But that's also the reason to use `hasOwnProperty()` in situations where it's a possibility. Have you ever heard of an application that failed because of this conflict? – Barmar Jan 05 '22 at 22:54
  • 1
    @Barmar It's not just chance, you also need to consider malicious actors, who pass keys such as `__proto__` on purpose. There have been quite some vulnerabilities caused by [prototype pollution](https://portswigger.net/daily-swig/prototype-pollution-the-dangerous-and-underrated-vulnerability-impacting-javascript-applications) - see also [here](https://stackoverflow.com/q/57960770/1048572) or [there](https://book.hacktricks.xyz/pentesting-web/deserialization/nodejs-proto-prototype-pollution). Even `hasOwnProperty` [needs to be used correctly](https://stackoverflow.com/q/12017693/1048572). – Bergi Jan 06 '22 at 00:44

1 Answers1

2

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375