4 ms·
This 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 ob
by spaethnl 10y ago
This 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.