Atoms are lazily evaluated. If an atom is created using a state initializer function, this function won't run until the
.value getter is read, or the atom is subscribed for the first time.
You can make use of this feature to avoid expensive computations where possible.
Lazy evaluation in derived atoms
A derived atom is not much different than a classical atom. Still, its state initializer function will wait for the atom's value to be requested in order to run.
Later, when it's consumed for the first time:
Dependency collection in derived atoms
Dependency collection is another performance optimization that makes lazy evaluation much more advanced.
When an atom is evaluated, it collects its latest dependencies. Since the
$sum is evaluated at least once in our previous example, it's now "aware" that it's dependencies are
$beta. Let's observe what will happen when those dependencies are updated:
$sum knew that it needs to rerun its state initializer when it's
.value is requested after the dependencies are changed. This can happen thanks to dependency collection.
$sum knows that its internal state is invalid without causing evaluation. It can avoid evaluation until it's essential.
Lazy evaluation in atoms created with
Same kind of performance optimizations apply to the atoms that are created using the
xoid supports special kind of atoms called "stream"s. A stream is "an atom that may or may not have an immediate value". Lazy evaluation works slightly different in a "stream". See the next section for more.