3 ms·
I don't use Lodash, but I remember reading that it does lazy evaluation[1]. For example, if you map an array, then take the last 3 items, it only calls the map
by acbabis 10y ago
I don't use Lodash, but I remember reading that it does lazy evaluation[1]. For example, if you map an array, then take the last 3 items, it only calls the map function for those 3 items.
Does anyone know if V8 can do any optimizations like this for native Array functions? That would be a good thing to know before downloading lodash.
1) http://filimanjaro.com/blog/2014/introducing-lazy-evaluation/ http://filimanjaro.com/blog/2014/introducing-lazy-evaluation...
- spaethnl 10y agoThis cannot be done with native array functions as they exist now. The reason Lodash is able to do this is because the collection is wrapped in an a special object that keeps track of all of the functions you call on it. Those functions don't run until you call .value() on the whole pipeline that you have setup, and then the optimization happens. Note that this requires a separate API. One API returns your results immediately, the other (lazy) API instead returns a wrapper object after each call without yet doing the mapping/filtering/etc, ready to add more instructions to the pipeline that you must ultimately call .value() on in order to actually run. Native functions don't have this separate lazy API that does the wrapping with this special object, and don't have/return an object with a similar .value() method.