# Add enumeration methods (and clear) to WeakMap and WeakSet

**URL:** <https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564>\
**Category:** 💡 Ideas\
**Created:** [December 2, 2020, 7:15pm UTC](https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564 "2020-12-02T19:15:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbiespeed](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/robbiespeed/32/2544_2.png) [@robbiespeed](https://es.discourse.group/u/robbiespeed)\
**Post date:** [December 2, 2020, 7:15pm UTC](https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564/1 "2020-12-02T19:15:21Z")

</div>

Since WeakRef has been introduced it is now possible to create enumerable weak collections (one example in [WeakRef Proposal](https://github.com/tc39/proposal-weakrefs#iterable-weakmaps)). However since they are not built-ins they require the addition overhead of creating multiple private collections to back the storage and a finalizer to clear it.

I propose that WeakMap/WeakSet match the full api of Map/Set. Adding: clear, Symbol.iterator, entries, keys, values.

Would their be any reasons not to do this, such as possible reductions in performance for existing WeakMap and WeakSet?

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [December 2, 2020, 7:36pm UTC](https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564/2 "2020-12-02T19:36:24Z")

</div>

I do think supporting these methods would decrease the performance of WeakMap and WeakSet. So should perhaps be added as a new separate class? `new EnumerableWeakMap`. Right now v8 implements WeakMap/Set by storing the hash in the key Object itself, so there is no direct way to go from the WeakMap/Set back to the object without searching all objects for one with a matching hash.

> **[Optimizing hash tables: hiding the hash code · V8](https://v8.dev/blog/hash-code)**
>
> Several JavaScript data structures such as Map, Set, WeakSet, and WeakMap use hash tables under the hood. This article explains how V8 v6.3 improves hash table performance.

There are also some details about possible secuirty implemations of enumerable WeakMap/Set

> A key property of Weak Maps is the _inability to enumerate their keys_ . This is necessary to prevent attackers observing the internal behavior of other systems in the environment which share weakly-mapped objects. Should the number or names of items in the collection be discoverable from the API, even if the values aren't, `WeakMap` instances might create a [side channel](http://en.wikipedia.org/wiki/Side_channel_attack) where one was previously not available.

[http://tc39wiki.calculist.org/es6/weak-map/](http://tc39wiki.calculist.org/es6/weak-map/)

---

<div class="post-metadata">

**Author:** ![robbiespeed](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/robbiespeed/32/2544_2.png) [@robbiespeed](https://es.discourse.group/u/robbiespeed)\
**Post date:** [December 2, 2020, 8:30pm UTC](https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564/3 "2020-12-02T20:30:08Z")

</div>

The performance implications are a concern, so new `EnumerableWeak` classes seems the best option.

As far as security goes WeakMap/Set methods can already be overridden to allow 3rd party observation. Either you need to freeze the instance and it's prototype, or not expose the WeakMap/Set directly, by using wrapper functions for setting and retrieving values. The second of which would still be a good option for enumerable collections.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 2, 2020, 8:55pm UTC](https://es.discourse.group/t/add-enumeration-methods-and-clear-to-weakmap-and-weakset/564/4 "2020-12-02T20:55:10Z")

</div>

Or, you can preserve and call-bind the prototype methods in advance.

That you can modify the prototype doesn't change that it's critical that WeakMap and WeakSet _never_ expose the keys or members unless you already have them.
