leisure
and those non-lazy libraries is the guarantee that no computations will happen unless the result is actually used. Here's an example:map
across the entire list of users. The code will parse 100 addresses. In this trivial example, that computation is likely very fast, but imagine if it were more intensive (say, using computer vision in the browser to detect a landmark). The filter
will then also run 100 times to find the subset of addresses we are looking for. Then the second map
will run 100 more times (admittedly convoluted). The total time of the computation is O(3N)
.map
would one once on the first user, that resulting address would run once through the filter
and pass, then that address would run once more through the second map
. The total computation would be O(3)
. The worst case computation, where there is only 1 match and it is at the very end would be as slow as the original, but every single other permutation would be faster using the lazy approach.leisure
:first
helper method.2000_01_01
)..first
and .find
methods we've been using are examples. They take a Sequence and return a normal JavaScript value.toArray
, forEach
, sum
, sumBy
, average
, averageBy
, frequencies
and groupBy
..toArray()
to flush the results.leisure
caps the maximum number of infinite values to 1_000_000
. This number is configurable by setting Seq.MAX_YIELDS
to whatever you want your max loop size to be.leisure
implements the Iterator protocol which means you can use it to lazily pull values using a normal for
loop.leisure
can be used as a drop-in replacement for the ES6 map
/filter
/reduce
API. If that's all you need, you can still benefit from the reduction in operations in the non-worst-case scenarios.leisure
's helper methods, you'll be able to write more expressive data computations. With the ES6 API, developers often drop into reduce
for more complex data transformations. These reducer bodies can be difficult to read and understand how they are actually transforming the data. leisure
provides methods which do these common transformations and names them so your code is more readable. Dropping all the way down into a reducer is quite rare in leisure
code.yarn perf
from the repository to reproduce these numbers.