People like to use chain methods like .map, but these are sometimes not efficient.
I just made a program that iterates using lazy approach.
- The result array is evaluated only at the end of chains (with
.collectmethod) - The difference is that the native method creates every single result arrays for each chain process.
- There is no full compatibility with the native methods.
After I took benchmark, I noticed that:
- native
.mapis more performant when the size of an array is small. - my
.mapis more performant when the size of an array is very big.
usage:
import { chain } from 'chain.ts'
chain([-10,200,3,400,-50])
.map(v=> v*2 - 30)
.filter(v => v > 0)
.map(v=>`yaay ${v}`)
.collect()You don't need to call .collect() if you call forEach, some, or find at the end of the chain.