1

The few times that I've used objects as keys in a map I did it to store metadata about that specific object. I've always used a WeakMap for this because of the benefit of the entry in the map being garbage collected automatically when the object it was using as a key gets garbage collected.

Storing metadata is the only use case I can think of for objects as keys in maps, but I'm curious if there are other use cases for objects as keys and if in those cases, you'd use a regular Map over a WeakMap.

m0meni
  • 14,160
  • 14
  • 66
  • 120
  • possible duplicate of [What's the difference between ES6 Map and WeakMap?](http://stackoverflow.com/q/15604168/1048572) or [What are the actual uses of ES6 WeakMap?](http://stackoverflow.com/q/29413222/1048572) – Bergi Jun 02 '16 at 15:44
  • @Bergi I don't think this is a duplicate of either since I know the difference between Map and WeakMap, and I state a use of WeakMap in the question itself. It's more about knowing when you'd use Map over WeakMap, which isn't covered as much as the opposite (use WeakMap over Map) really. – m0meni Jun 02 '16 at 15:46
  • 1
    Just take a look at the restrictions of WeakMaps. They're not iterable, for example - so if you need that, you'll want an `Map`. – Bergi Jun 02 '16 at 15:51
  • @Bergi I guess I'm just curious as to when you'd ever come across that scenario. I understand things better when I can relate them to something I'd actually do, but when would you ever need to iterate over metadata? – m0meni Jun 02 '16 at 15:57
  • Basically when you need more browser support, be able to know the size, be able to iterate over existing keys or be able to have primitives keys such as string or numbers, you should use map. You could also test if it is faster/slower than map – juvian Jun 02 '16 at 16:27
  • 2
    Consider an implementation for modeling graphs. Lets assume nodes of the graph can be user defined objects. The graph implementation needs to store those nodes but also associate them with other data (such as edges (think of an adjacency "dictionary")). We would need a `Map` since we have to be able to iterate over all nodes as well (that's e.g. what https://networkx.github.io/ does (in Python) and my JS port of it). – Felix Kling Jun 02 '16 at 16:46
  • @FelixKling ah perfect. That was exactly what I wanted thank you! – m0meni Jun 02 '16 at 16:47
  • 2
    And even if we didn't need to iterate over the nodes, we probably wouldn't want them to be garbage collected if there are no other references to them, since that would silently destroy the graph. – Felix Kling Jun 02 '16 at 16:49

1 Answers1

2

Consider an implementation for modeling graphs. Lets assume nodes of the graph can be user defined objects. The graph implementation needs to store those nodes but also associate them with other data (such as edges (think of an adjacency "dictionary")). We would need a Map since we have to be able to iterate over all nodes as well (that's e.g. what networkx.github.io does (in Python) and my JS port of it)

And even if we didn't need to iterate over the nodes, we probably wouldn't want them to be garbage collected if there are no other references to them, since that would silently destroy the graph.

- Felix Kling

m0meni
  • 14,160
  • 14
  • 66
  • 120