Weak References

Jordan Harband ljharb at gmail.com
Sun Sep 27 18:39:02 UTC 2015


`WeakMap` holds the *key* weakly, not the value - it holds the value
strongly. If you have the key such that you can use it to get at the value,
you'll always have the value.

`WeakRef` is a container around a weakly held *value*, which I don't
believe is possible to fully polyfill, even with `WeakMap`/`WeakSet`
(because they are not enumerable).

On Sun, Sep 27, 2015 at 9:09 AM, Herby Vojčík <herby at mailbox.sk> wrote:

> A naive question: don't you get working weakref by using a WeakMap and
> getting and setting using the same key? A naive point of view suggests
> that. What am I not seeing here?
>
> Herby
>
> Isiah Meadows wrote:
>
>> I'm resurrecting this [1] because I have found a use case where I needed
>> a weak reference. Is there any chance this could get into the language?
>>
>> Here's my particular use case: making a stream with a rotating
>> destination that itself acts as a stream (snippet of that code, or what
>> I'd like it to be).
>>
>> ```js
>> function rotateStream(interval, format) {
>>      // Without the weak reference here, there's a memory leak
>>      const ref = new WeakReference({})
>>      setStream(ref.get(), format)
>>      setInterval(function () {
>>          if (ref.exists()) setStream(ref.get(), format)
>>          else clearInterval(this)
>>      }, interval)
>>      return ref.get()
>> }
>> ```
>>
>> This basically rotates an active stream, with the weak reference
>> pointing to the said stream. The alternative requires explicit marking
>> (in this case, with a `close` method):
>>
>> ```js
>> function leakyRotateStream(interval, format) {
>>      let stream = {close() {
>>          clearInterval(timer)
>>          timer = stream = null
>>      }}
>>      const timer = setInterval(function () {
>>          setStream(stream, format)
>>      }, interval)
>>      setStream(stream, format)
>>      return stream
>> }
>> ```
>>
>> In this use case, weak references would simplify the implementation and
>> reduce memory costs. And I would rather take advantage of the GC
>> machinery than explicit memory management (as explicit as C), as it
>> would be easier to collect when that's the only thing referenced.
>>
>> (The setInterval callback is gc'd with all its references when it's
>> cleared from within.)
>>
>> Thankfully, I'm using Node.js, so `weak` [2] is an option (the only
>> option, really). But I would definitely appreciate if it was available
>> in JS proper, across engines. V8, SpiderMonkey, and JSC all have weak
>> references to JS objects as part of their public API, and I would be
>> surprised if the implementation work would be anything significant.
>>
>> [1]: https://esdiscuss.org/topic/what-is-the-status-of-weak-references
>> [2]: https://github.com/TooTallNate/node-weak
>>
>> --
>> Isiah Meadows
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150927/3186280e/attachment-0001.html>


More information about the es-discuss mailing list