Immutable.js: An Introduction with examples written for humans
untangled.io
untangled.io
Now neither of them are really maintained.
For a lot of usages, something like Ramda or Lodash/FP will do the trick (Ramda lenses or Lodash/FP's get and set methods work great to handle immutable data structures), but if you really wanted structural sharing and stuff, now you're kindda stuck.
I went through the pull requests and checked 5 or so that were open for a long time, all of them were glaringly missing something.
Some do not pass tests, some have obvious styling issues, some people haven't signed the contributor agreement, some offer big changes with very little reasoning behind it.
I also prefer projects that are more explicit about the reason something is rejected and answer RTFM-type issues, but this is not a requirement to be a successful project.
Instead I ended up just deepFreezing my data (only in development) and wrote a bunch of pure functions which perform all the important array/object functions but return a new array/object instead of mutating. It was a really great exercise in functional programming and I'd recommend it to anyone wanting immutable data and to understand immutable programming styles.
They are all just a few lines, so they make for great practice to write yourself.
Second, TypeScript should be considered a best practice for nontrivial JavaScript development in 2016. And pretty much any app that would benefit from Immutable.js would be "nontrivial" by that definition.
Third, everyone using JavaScript or TypeScript knows what a Map is, I would hope. The docs for it that read "Immutable Map is an unordered Iterable.Keyed of (key, value) pairs with O(log32 N) gets and O(log32 N) persistent sets." are perfectly legible to me, having never used Immutable.js, just because I had a few undergrad (not "a Ph.D.") computer science classes. I don't even have a computer science degree [0] at all.
I actually personally despise lowest-common-denominator docs: Docs that tell me "what a Map is and how to use it" when what I want to know are things like what its get and set performance implications are, and whether you can iterate over it, and whether that iteration will be ordered, and so forth.
Sure, create a user's guide, but don't pick on the reference manual for being concise.
FWIW, I'm not a big fan of Immutable.js or immutable structures at all. I'm too performance-oriented to want a layer like that between me and the underlying data structures. Criticize it for being unmaintained [1], sure, but not for having docs that aren't aimed at beginners.
[0] My degree is a B.S. in Cognitive Science [1] https://news.ycombinator.com/item?id=13051458
For your comment about performance, immutable structure being comparable by reference can provide huge gains in read and re-use heavy environments, thanks to memoization-like techniques.
Fair enough. I'm often doing things to graphics, and the concept of using immutable data when the data is a bitmap with 4Mb of pixels, and you're trying to draw hundreds of sprites or shapes to it...well, let's just say that immutable doesn't make sense for that.
Even when I'm just working on a game, though, the more frequent allocations required by copy-on-write structures means more fragmentation of the heap and more deallocations later. Ideally during runtime nothing gets allocated.
You may even be right that the amortized speed of using immutable structures is the same as doing it with mutable data. But anything that adds a lot of allocations and therefore adds to the frequency of garbage collection will cause (or increase) jankiness in a game.
You have 16.66ms to accomplish all the work for a frame. If a garbage collector comes along and steals even 10ms, if you can't do the remaining work in 6.6ms, it will skip a frame, and users will see it.
And my current game needs to run in a browser, at least mostly. So here I am. :)
Then have a load of general examples to cover the non-experts and initiates. There's no faster way to learn how to begin using a library than to look at some complete examples. That's the one thing that Immutable documentation misses. I wish there was a "Show example" link on almost every non-trivial method.
A map from hashable keys to values. A map cannot contain duplicate keys; each key can map to at most one value. A HashMap makes no guarantees as to the order of its elements.
The implementation is based on hash array mapped tries. A HashMap is often faster than other tree-based set types, especially when key comparison is expensive, as in the case of strings.
Many operations have a average-case complexity of O(log n). The implementation uses a large base (i.e. 16) so in practice these operations are constant time.
It seems like websites are dead pretty often on HN, but on sites like Reddit where I assume there is a magnitude more traffic they seem to hold up pretty well (with some exceptions).
So what's going on here? Does HN really have a bunch of silent viewers? Or is it something more benign like sites that get submitted are often smaller?
+ Most of my time on Reddit is specific (small-ish) subreddits, vs. HN where you can basically just look at the front page.
With the multiple-magnitude less comments and votes that HN submissions get, I can't imagine they can get anywhere near that unless HN has a LOT more non-contributing types.
A lot of people still think using WordPress or even Rails for site generation is fine, but both are hard to scale. And if the site is small or academic, it will fall over with not too much traffic.
I wrote a rant [1] about this a couple of weeks ago. I can guarantee that my site will never have a "Database Connection Failed" error (now), because there's no database backing it.
Even better would be if I moved hosting to S3/CloudFront. Dirt cheap, and Amazon isn't going to fall over even if a site hits Reddit's front page. I might not like the bill I get at the end of the month if it really goes crazy, though.
[1] https://realmensch.org/2016/11/22/drupal-is-dead-long-live-s...
You must be pretty young to not remember it happening to lots of sites regularly for a couple of years.
Part of the reason imgur took off was because people would repost content to imgur when Reddit took the source down, the web comic or whatever.
I was just curious about the reason why. Like does HN have a large "silent" group, or is it something like that on HN sites climb much faster so I see them when they are down before they've had a chance to recover.
Hmm I wonder if there are any good traffic statistics. I had a library (msngr.js) hit the front page once over a year or so ago and while it helped me gain a bunch of GitHub stars looking at the traffic itself it wasn't really much at all. In fact it was significantly less than I would have expected. Granted maybe my library just wasn't that interesting to people but I would love to see somewhere with some good HN numbers.
When I posted a recent high ranking blog post[0], I received around 18,000 views over the next 24 hours according to Google Analytics. I don't believe this covers a lot of the bots and associated traffic that also crawled the site.
This amount is nothing for a Jekyll site, I had CPU hovering at around 4%, with a series of services also running on this server. This is, imo, where a "hug of death" should end.
I did however at the time, have several colleagues in web development express shock and awe at the fact the site stayed online with traffic levels such as this. Multiple people tried to school me on the need for Cloudflare at this incredible scale.
A lot of web developers think about "scale" in terms of many bloated CMS platforms, and it is with no surprise that the site in question is running Wordpress.
We had a friend's site a while back where we worked out, with just two desktops and spamming "F5" on the keyboard, we could take the site offline, and take several minutes to recover from after the fact. He went and posted for help on Reddit, and sure enough, the consensus was that his experience involved far more traffic than any website could reasonably be expected to handle.
Your answer therefore lies not in the silent HN users, but in the unusually poor performance of popular CMSs. And although I'm using Wordpress, it's not alone in this situation. [0] https://news.ycombinator.com/item?id=12973181
I'm saying that the frequency I see the "HN hug of death" is much more than the "Reddit hug of death" while having many of the same kinds of sites (the "bloated CMS platforms" you talk about).
Makes it very simple to use it anywhere you'd use a plain array or object, rather then always having to use immutable.js's api.
I.E. if I have {a:{aa:"1"},b:"cc"} and I change b, then {aa:"1"} will not be deep copied.
Are you talking about something like keeping a reference to the parent and sending lookups up the chain (with a prototype or some such?)
> Persistent data structures are different, as their performance improvements are passive. Although seamless-immutable does not (and cannot, while maintaining its backwards compatibility with vanilla JS collections) use things like VLists under the hood, its cloning functions—such as merge—only bother to make shallow copies, as shallow and deep copies of immutable values are equivalent. In practice, this simple passive optimization has been sufficient; we have yet to encounter a performance problem that Bagwell-style persistent data structures would have solved.
from: http://tech.noredink.com/post/107617838018/switching-from-im...
a \
c-d-e
b /
The memory for the original list is still shared!
You can generalize that to trees and use that to implement, say, persistent hashmaps that can share data.This is also awesome for parallelization where you want to share data but don't need synchronization!
I'm not convinced this library is really that useful in a dynamic language. Considering the top 2 immutable libraries (Mori and now Immutable.js) are both essentially abandoned I get the feeling the public opinion is likely the same.
If you are less concerned with pure functions and happy with largely imperative or hybrid imperative code, then yes it is difficult to find a compelling example for Immutable.
(Immutable seems almost entirely stable at this point and covers just about the entire necessary API surface, so I think it's a case to call it "mature" rather than "abandoned".)
Meh, I don't see a lot of usage even with pure functions. JavaScript never really had any protections in it (minus the new-ish const and Object.freeze()) so most developers I know simply write as if something else has access to an object then it will mess with it so everything gets architected accordingly. Just seems to me a mostly non-issue in the JavaScript world.
> so I think it's a case to call it "mature" rather than "abandoned".
No I would most certainly call it abandoned. They have countless issues and pull requests from over half a year ago without even a single response from a human. While it may be a mature product, I would still call it abandoned.
For what it is worth, anecdotally, I've been doing a lot of heavy work with RxJS observable pipelines (in a CycleJS-based application) where Immutable has absolutely made a big difference in performance and stability of complex "four dimensional" (lots of changes over time) "shared nothing" (pure) state tracking.
That may be a use of the language and a complex learning curve of a framework setup in which you never plan to work, but it's definitely an "issue" where libraries like Immutable and Mori turn out to be extremely useful (whether or not you think they are abandoned).
I've heard similar anecdotes from people using various combinations of things like React and Redux with Immutable.
@alexhayes it is a common color palette, I was going to link the most common place where I find it but I forgot about it, haha.
The author spoke about performance and immutability-data structures like Directional Acyclic Graphs and Trie.