getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
179.26k stars 33.48k forks source link

fix: more complete statement for Map #1773

Open DBadalyan opened 2 years ago

DBadalyan commented 2 years ago

Please type "I already searched for this issue": I already searched for this issue Edition: (pull requests not accepted for previous editions) 2-nd edition Book Title: Get Started Chapter: Chapter 3: Digging to the Roots of JS Section Title: Iteration -> Iterables Topic: Fixed statement about Map keys.

getify commented 2 years ago

The assertion in the text comes from:

  1. Using a Map with primitive keys is generally wasteful, as it's probably less efficient than just using a normal object. The conventional wisdom is, use a plain object unless you need object/function keys, in which case you use a Map. I didn't explain that nuance in the text, but I agree with that opinion and based my statement on it.
  2. The WeakMap(..) counterpart to Map(..) does in fact require only objects/functions as keys, so it's a frustrating gotcha if you've been in the habit of storing lots of non-object keys in Map and then you try to do so with a WeakMap and it fails.
DBadalyan commented 2 years ago

I totally agree with your arguments, and they are indeed very important. I just disagree with an idea to have that statement in that way so it can be misused somehow via referencing.

I understand that this is not a reference book and despite that, it is even important to have very clear and complete statements, considering book's popularity among beginners.

Might be somehow it can be introduced by other way which already will emphasize notions you listed above.

Thanks for replay.

PS: not a big deal actually, just trying to be idealistic.