Hacker News new | ask | show | jobs
by chrismorgan 1992 days ago
Map is ES6. Everyone had implemented it by late 2014. (Even IE11 has it.)

https://caniuse.com/mdn-javascript_builtins_map

1 comments

Nitpick: IE11's implementation of Map isn't iterable, which makes it largely useless for practical purposes. Real-world codebases that use Map and care about running on IE11 polyfill it.
That’s more about [@@iterator] not being a thing on IE11 rather than anything about Map. There’s still Map.prototype.forEach, which is entirely sufficient.

Also, iteration is not the only purpose of Map; its real value is that you can use keys of any type, and I’ve used Map and WeakMap a number of times without having needed any form of iteration—e.g. avoiding polluting every element I need to track data on (MY_DATA: Symbol; Element[MY_DATA]: MyData), maintaining a WeakMap<Element, MyData> instead.

One thing I like about JS is that you can monkeypatch anything. And if you want all objects of any type to have the property/method you just add it to the prototype.
Except that really doesn’t pan out well here: it’s not just one or two things that need patching, it’s a lot, and the patching will slow some extremely common operations down quite a bit (sometimes immensely), and it’ll still be incomplete because there are syntax elements to it (e.g. for..of) that can’t be polyfilled.

Thus, for as long as you want to support a certain vintage of browser, you’re very commonly better to just write things in the old way—which here means using Map.prototype.forEach instead of Map.prototype.{entries, keys, values} and/or for..of loops.