JavaScript and Maps (in that order)

Weak Maps ES6

After talking to Rick Waldron I think I finally understand the deal with WeakMaps. Here it is:

The keys of a WeakMap are garbage collected, that means if you use an object as a key and that object can no longer be reached they key and the value are collected. This is in contrast to an object or a Map as they will never be garbage collected. This garbage collection property isn’t necessarily good or bad, depending on what your using it for it could hurt you or it could help you.

In order to garbage collect they keys WeakMaps are not iterable, the trade off here is that you can have truly private values in public facing things. Keys in Maps or WeakMaps can must be any object, but they have to be literally the same object not just an equal object, so if I I have a publicly facing WeakMap, but they key to a value is inaccessible inside a closure, it can’t be accessed outside the closure.

In general I think the main issue with them is the name, for two reasons:

  • to paraphrase Crockford “nobody wants to use something that’s weak”
  • It is very similar to Map which is also being added (not to be confused with the map that was added in ES5…no it’ll be confused), before yesterday I had in my head they were like Map, but you couldn’t iterate which gave you some sort of memory/performance improvement. 

Rick Waldron suggested ‘Ephemeral’ ’Ephemeron’ as a name, though that’s a bit long, and we know how that can cause regrets plus it as name could also be deceptive though no name is going to be perfect. The best bet would be to call it by a synonym for Map like Dict or Hash and tell the CS people who complain 

In JavaScript the Dict is garbage collected, the Map isn’t, and I don’t give a fuck how Java/C#/F#/Haskel/Rust/Lua/Prolog/Erlang/Lisp/Clojure/Scheme/Dylan does it. 
  1. iamaboutus reblogged this from cwmma and added:
    Sounds good, though it’s difficult to remove names from that context. A lot of people are going to use “Dict” expecting...
  2. cwmma posted this