fp-ts icon indicating copy to clipboard operation
fp-ts copied to clipboard

Question: How to use with real immutable data structures (for performance)

Open bitc opened this issue 4 years ago • 1 comments

Hello, thank you for this library, it seems very well designed.

But I noticed that the implementation doesn't use real immutable data structures.

For example, I was looking at the code for Set. It seems to be implemented using the native JavaScript Set, and looks to be very inefficient. The code for the elem function loops over all of the elements in the Set, comparing each one for equality.

I would expect the Set data structure to have an Ord constraint, and be implemented internally using a balanced tree, so that we could have O(log n) lookup, instead of the current O(n).

I think the Map data structure here has similar problems, and also it doesn't look like there is any efficient List data structure (like Haskell's Data.Sequence).

There are lots of immutable data structure libraries in the JavaScript/TypeScript ecosystem, but they all have bad APIs. Is this library meant to be used together with them?

Thank you

bitc avatar Apr 21 '21 16:04 bitc

It seems to be implemented using the native JavaScript Set, and looks to be very inefficient...I think the Map data structure here has similar problems

That's right, the purpose of those modules is just to provide common functional abstractions for the native JavaScript objects (despite their inherent inefficiency).

There are lots of immutable data structure libraries in the JavaScript/TypeScript ecosystem, but they all have bad APIs. Is this library meant to be used together with them?

It's possible to write bindings, here's an example: fp-ts-rxjs

gcanti avatar Apr 21 '21 16:04 gcanti