r/javascript • • Oct 11 '17

JavaScript's Promise is Memory Unsafe

https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html
70 Upvotes

77 comments sorted by

View all comments

0

u/[deleted] Oct 11 '17

[deleted]

-18

u/alexelcu Oct 11 '17

As I mentioned in the article, an implementation that doesn't leak is possible.

So obviously the assertion about creating an "infinitely long promise chain ... means nothing can be GCed" is false.

To see it in action, do this:

npm install funfix --save

And then:

import { Future } from "funfix"

function loop(n) {
  return Future.of(() => n).flatMap(i => {
    if (i % 1000 == 0) console.log(i)
    return loop(n + 1)
  })
}

loop(0).recover(console.error)

This code is doing exactly the same thing, but it doesn't leak.

This is sort of a Stockholm Syndrome btw :-)

1

u/pier25 Oct 23 '17

So obviously the assertion about creating an "infinitely long promise chain ... means nothing can be GCed" is false.

How is that? If you keep a reference to something then it won't be GCed...

This code is doing exactly the same thing, but it doesn't leak.

Well, then it's not doing exactly the same thing. Does it?

1

u/alexelcu Oct 24 '17

There is nothing in the Promise API that says references must be kept in such chains — it's quite interesting how people jump to conclusions on how the implementation should behave, when the API itself is free of such constraints.

In fact intermediate references can be dropped, implementations are known, TFA now includes mentions of such implementations, which are widely deployed and verifiable. TFA also includes acknowledgements from the authors of the Promise spec and others.

And I think I've been pretty clear on that. If not, then let me know how the explanation in TFA can be improved.