It may be April Fool’s day, but these links are no joke. To be sure, I wrote this article on March 31st. ;) However, to mark the occasion, I’m going to have four links today instead of three. Mind blowing.
My theme for these links is immutability. Specifically immutability in JavaScript. Arrays and objects, as well as the new Map
and Set
in JavaScript, are mutable. That is one can change their values and contents while maintaining a single reference to them. While this is a familiar concept for some, it sometimes has unintended side effects when these entities are passed around to different parts of the application.
On the other hand programming with immutable data types requires a slightly different way of thinking about things, but offers benefits which make it valuable in certain applications.
For example, change detection. If you want to know if a JavaScript array has changed, you could first check its length, but if they are the same, you’d have to iterate the whole array to determine if any element has changed. And even then, for mutable types within the array, you’d have to check those deeply as well. But if you have immutable types, then checking for a changed array is just a reference check which is much faster.
Most of these libraries listed do very similar things but have different syntax, different performance and manage slightly different data.
Immutable.js
https://facebook.github.io/immutable-js/
Immutable collections for JavaScript from Facebook. Instead of extending the native types, Immutable has its own collections: List, Map, OrderedMap, Seq, Stack, etc. They are all immutable and can be created from and cast back into native arrays and objects as necessary. The API, while a bit verbose, is clean and understandable. If you want some addition data and collection types in addition to immutability, then this is a good choice.
Mori
https://github.com/swannodette/mori
Mori is designed to emulate ClojureScript’s persistent data structures in JS. As such it is decidedly more functional (stylistically) than the rest of the options. It supports many of the same data types as Immutable.js but claims to be faster (but larger). It has tried to make provisions for being closer to idiomatic JavaScript, but to be honest, it still feels a bit foreign. YMMV.
Ancient Oak
https://github.com/brainshave/ancient-oak
Ancient Oak dubs itself as the immutable tree. I like the name of this library the best, but that is judging a book by its cover. It is designed to work on trees of plain JS data types, something you might get from or convert to JSON. One of the types supported by Ancient Oak is the Date object, which isn’t something I’ve seen in any of the others. Allows you to use the types that you are used to in JS with pretty much the same API.
Freezer
https://github.com/arqex/freezer
Freezer is a bit like Ancient Oak in that it maintains an immutable tree structure, but it was built with React in mind, so it also has an event mechanism which allows you to re-render your application when the freezer has been updated. That said, it doesn’t seem to have immutable Date support. And having played with Freezer and Ancient Oak, the API seems a lot more pleasant and intuitive with the former.
There are no comments available on this site. If you want to respond then you can write your own blog article and send me the link via twitter. If there is an issue with this article or the site in general, you can open an issue on Github