Why have I spent six pages talking about JavaScript and React in a Clojure book? I promise I'm not trying to waste your precious time; we simply needed some context to understand what's to come.
Om[4] is a ClojureScript interface to React.js that was developed by the prolific and amazing individual, David Nolen, from Cognitect. Yes, he has also developed core.logic, core.match, and the ClojureScript compiler. That's how prolific. But I digress.
When Facebook released React, David immediately saw the potential and, more importantly, how to take advantage of the assumptions we are able to make when programming in Clojure, the most important of which is that data structures
don't change.
React provides several component life cycle functions that allow developers to control various properties and behaviors. One, in particular, shouldComponentUpdate, is used to decide whether a component needs to be re-rendered.
React has a big challenge here. JavaScript is inherently mutable, so it is extremely hard when comparing virtual DOM trees to identify which nodes have changed in an efficient way. React employs a few heuristics to avoid O(n3) worst-case performance and is able to do it in O(n) most of the time. Since heuristics aren't perfect, we can choose to provide our own implementation of shouldComponentUpdate and take advantage of the knowledge we possess when rendering a component.
ClojureScript, on the other hand, uses immutable data structures. As such, Om provides the simplest and most efficient implementation possible for shouldComponentUpdate: a simple reference equality check.
Because we're always dealing with immutable data structures, to find out whether two trees are the same, all we need to do is compare whether their roots are the same. If they are, we're done. Otherwise, descend and repeat the process. This is guaranteed to yield O(log n) runtime complexity and allows Om to always render the UI from the root efficiently.
Of course, performance isn't the only thing that's good about Om—we will now explore what makes an Om application.
Since the first edition, a new version of Om called Om Next was introduced. Om Next borrows ideas from Facebook's Relay[5], Netflix's Falcor[6], and Cognitect's Datomic[7]. We will mention how these technologies influenced Om Next in the following subsections. Unless otherwise stated, we will use the term Om to mean Om Next.