12 ms·
The lodash equivalent, which I missed, is something like this: _.chain(inputSet) .filter((x) => x >= 0) .map((x) => x * 2) .thru((e) => new
by arh68 5y ago
The lodash equivalent, which I missed, is something like this:
_.chain(inputSet)
.filter((x) => x >= 0)
.map((x) => x * 2)
.thru((e) => new Set(e))
.value()
I think it looks fine, works fine.
- rco8786 5y agoTo be clear, this is plain javascript. The only lodash does here is the initial _. And you’re right, it does work just fine.
- webstrand 5y agoThat's not plain Javascript. That's Lodash through and through. The array transformation methods built into Javascript run to completion after every step building multiple copies of the array as each transformation is applied. Lodash's chain and related methods do not make multiple copies of the array during transformation.
- bunnyfoofoo 5y agoHey, do you have more documentation on how vanilla Javascript handles array transformations? As an example: array.filter(f => true).map(f => f); My understanding is that filter creates a new array, then maps that new array to another new array in final step. Does lodash do something differently? I'm not seeing anything in documentation for _.chain that says it does something differently.
- eurasiantiger 5y agoIt’s best to avoid _.chain, see https://medium.com/bootstart/why-using-chain-is-a-mistake-9bc1f80d51ba https://medium.com/bootstart/why-using-chain-is-a-mistake-9b...
- e12e 5y agoHow so? Looks like filter builds a new array, and so does map? And both while-loops over the entire input? https://github.com/lodash/lodash/blob/master/filter.js https://github.com/lodash/lodash/blob/master/filter.js https://github.com/lodash/lodash/blob/master/map.js https://github.com/lodash/lodash/blob/master/map.js With a quick search I found this: https://hackernoon.com/es6-lodash-native-javascript-nodejs-example-tutorial-map-function-functional-programming-performance-speed-69645e613049 https://hackernoon.com/es6-lodash-native-javascript-nodejs-e... Which seems to indicate little difference?
- rco8786 5y agoI mean the continuation pattern used that effectively mimics the pipe operator. I realize that the functions themselves are provided by lodash.
- n_e 5y ago> I think it looks fine _.chain and .value() could be removed > works fine. - it isn’t possible to tree-shake the package and only include the lodash functions that are used - it isn’t possible to have non-lodash functions in the pipeline (e.g. date-fns)