Hacker News new | past | comments | ask | show | jobs | submit login

Note that you can have cheap equality detection by using e.g. flags or revision counters and so on (but there are limits due to JavaScript's nature..). The problem is that JavaScript currently doesn't support this properly out of the box, but Object.observe is coming in the next standard.

I personally like the safety benefits of immutability. You can give objects to functions and not worry about the functions changing the object. The only way to guarantee that otherwise is to clone the object, but that takes quite a bit of performance.

The difference is that if you use Object.observe to track changes, then you don't have the ability to hold onto the previous version of an object and know what the old value was.

This is important for animations where you want to animate from the object's old value to the new value.

The objects passed to the Object.observe callback function describe both the new value and the old value, FWIW.

Isn't this library probably cloning?

If you watch the linked video above you will see how clever persistent data structures mean that nothing is ever cloned or copied.

Good to know. I never got into immutable collections because I presumed they would result in things being allocated all the time (and that that would be expensive).

I'd guess this library clones on modification, but I was talking about cloning the object on every function call.

Persistent data structures allow for the appearance of cloning (you have a new thing) but are much more efficient behind the scenes (you don't actually copy everything). So if you find yourself doing a lot of defensive cloning, then Mori or this library would probably be a lot faster for you.

Absolutely! If anyone is reading this, I recommend Eric Lippert's 11 part series on immutability in C#. The concept apply everywhere, of course.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact