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

Show parent comments

11

u/bair-disc Oct 11 '17

But using heap memory and a stack should be an important difference. At least you've said, that Promises do pollute the heap memory, not the (call) stack. It would be nice to consider the difference - or to explain what is meant by abusing the heap memory.

1

u/alexelcu Oct 11 '17 edited Oct 11 '17

There are languages that don't use the stack at all, e.g. Haskell. In the larger scheme of things, the difference is not that big.

What I'm saying is that for functional programming, which helps with reasoning about the ordering of asynchronous / concurrent calls, there's no way to express loops (without mutation) unless you express them as tail recursive calls.

And it's really easy btw to end up with long loops that need to be processed recursively (TypeScript syntax to make the definition clear, but that's irrelevant):

type Task<A> = () => Promise<A>

interface List<A>

class Cons<A> implements List<A> { 
  constructor(readonly head: A, readonly rest: Task<List<A>>) {}
}

class Nil implements List<A> {
  constructor(readonly error?: any) {}
}

There. I've just described a potentially infinite, asynchronous, lazy, referentially transparent list.

Describing operations on this with async / await or callbacks works, sort of, but it's an exercise in frustration. And by using the Promise pattern as it was meant to be used, you end up with a blown heap.

7

u/bluebaron Oct 11 '17

What about this list makes it lazy or asynchronous? Or for that matter, referentially transparent? JavaScript is strictly evaluated, you can't even construct a value in that list unless you have the value to want to add to it already. I see no function signatures indicating callbacks for asynchronous operations either. There's nothing preventing one from mutating one of the references within the list, so I'm not sure where the referentially transparent claim is coming from.

1

u/alexelcu Oct 11 '17

What about this list makes it lazy

The fact that the rest of the stream is described with a parameter-less function, also called a thunk, which is how you turn strict things into lazy ones, obviously.

asynchronous

The fact that the rest of the stream can be processed with a Promise.

Or for that matter, referentially transparent?

The fact that side effects get suspended in that parameter-less function and there's no mutation involved for building it.

I see no function signatures indicating callbacks for asynchronous operations either

That's what the Promise is for. If it would work properly that is.

There's nothing preventing one from mutating one of the references within the list

I've modified the original definition to have readonly on those members, if that makes you happy :-)

6

u/bluebaron Oct 11 '17

I missed the Task<A> you had in the constructor signature the first time, my bad.

Also, I know what a thunk is, and your tone in these comments comes off as pretty condescending. I think you'd be better received here if you abstained from talking to people like they have no idea what they're talking about.

2

u/alexelcu Oct 11 '17

I apologize for sounding condescending or aggressive, I do try to explain things using less jargon (thunk is less understood jargon).

I'm also incredibly annoyed. Somebody here called me a scumbag for talking about a library I've been working on in my free time at personal sacrifice, so I don't necessarily know what I'm doing here, instead of going home to my wife and kid, but I guess I'll just pass by the bank first to retrieve my million dollars.

As you can see, my humor doesn't help much either.

Cheers,