QuickDevGuide

Home › JavaScript

Map, Set & Weak Collections — JavaScript

Plain objects and arrays are not always the best collections. Collection Keys Ordered Use for Object strings/symbols insertion (mostly) records, JSON Map any value (objects too) insertion dictionaries, caches, frequent add/remove Set unique values insertion de-duplication, membership tests WeakMap objects only, weakly held no private data / metadata without memory leaks WeakSet objects only, weakly held no "has this object been seen?" flags Why Map over Object for dictionaries? Map has a reliable size, accepts any key type, has no prototype keys to collide with ("constructor"), and is faster for frequent insertions and deletions. Set operations Modern engines support union, intersection, difference and isSubsetOf directly on Set. Weak collections Entries in a WeakMap do not prevent their…

Key Takeaways