Earlier quoted context omitted.
Yeah, years ago I got deep into this stuff and managed to implement a lot of it for fun, thinking I was doing something original, before discovering it’s not original (in my case, discovering Seq in OCaml). I think treating everything as lazy and potential infinite makes the implementation of operations much simpler than this article’s implementation.
I think for most usage, you’re right. There are places where it probably has more overhead than you’d want in a tight loop, and you’d be better off with the “bad” imperative version. E.g. “If a tree falls in the woods, does it make a sound? If a pure function mutates some local data in order to produce an immutable return value, is that ok?” I’m coming from a perspective where I now work primarily in JS/TS, prefer FP…
Lodash's Chain vs. Native Methods
11–20 of 25 posts
Re: Lodash's Chain vs. Native Methods
#12In the article when comparing the native vs lodash implementation the native one is given as: persons .map(p => ({ ...p, age: p.age + 1 })) .filter(p => p.age >= 30) .slice(0, 5); ^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e. person.filter(p => p.age >= 29) .slice(0,5) .map(p => ({...p, age: p.age + 1})
const drinkingAge = 21
persons
.map(p => ({ ...p, canDrink: p.age >= drinkingAge }))
.filter(p => p.canDrink)
.slice(0, 5);
Assuming we want `canDrink` to exist in those objects for later use, reversing the steps here would mean duplicating the logic and the work of computing it across the map() and the filter(), instead of keeping those concerns separate and re-using values: const drinkingAge = 21
persons
.filter(p => p.age >= drinkingAge)
.map(p => ({ ...p, canDrink: p.age >= drinkingAge }))
.slice(0, 5);
You could of course share the business logic by abstracting it into a function: const drinkingAge = 21
function canDrink(person) {
return person.age > drinkingAge
}
persons
.filter(p => canDrink(p))
.map(p => ({ ...p, canDrink: canDrink(p) }))
.slice(0, 5);
But you'd still be doing the work twice. In this example it's trivial, but in a real-world scenario it might not be.Re: Lodash's Chain vs. Native Methods
#13In the article when comparing the native vs lodash implementation the native one is given as: persons .map(p => ({ ...p, age: p.age + 1 })) .filter(p => p.age >= 30) .slice(0, 5); ^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e. person.filter(p => p.age >= 29) .slice(0,5) .map(p => ({...p, age: p.age + 1})
Re: Lodash's Chain vs. Native Methods
#14In the article when comparing the native vs lodash implementation the native one is given as: persons .map(p => ({ ...p, age: p.age + 1 })) .filter(p => p.age >= 30) .slice(0, 5); ^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e. person.filter(p => p.age >= 29) .slice(0,5) .map(p => ({...p, age: p.age + 1})
Re: Lodash's Chain vs. Native Methods
#15Re: Lodash's Chain vs. Native Methods
#16In the article when comparing the native vs lodash implementation the native one is given as: persons .map(p => ({ ...p, age: p.age + 1 })) .filter(p => p.age >= 30) .slice(0, 5); ^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e. person.filter(p => p.age >= 29) .slice(0,5) .map(p => ({...p, age: p.age + 1})
Consider that the initial idiom
const ps = persons
.map(p => ({ ...p, age: p.age + 1 }))
.filter(p => p.age >= 30)
.slice(0, 5);
is not only unoptimized in the sense that it does O(n) age increments, but also does O(n) object clones (which are far more expensive). It also does twice as many array allocation as the naive procedural approach: const ps = [];
for (const p of persons) {
if (p.age > 29) ps.push(p);
if (ps.length === 5) break;
}
If one wants to argue about readability, consider that the lazy approach requires understanding the semantics of both `chain` and `take` (in addition to all the mentioned downsides about extra code), whereas pretty much everything in the procedural approach can be found in a beginners JS course (or most other mainstream languages). And for an advanced developer's eye, the cost of the snippet is explicit.Takeaway: sometimes, the seemingly overly simplistic solution is the best choice, and conversely an overly complex solution is a product of a semi-irrational bias, rather than objective analysis.
Re: Lodash's Chain vs. Native Methods
#17Re: Lodash's Chain vs. Native Methods
#18Re: Lodash's Chain vs. Native Methods
#19Comparisons of lodash to native methods are interesting but they always miss the real selling points of lodash in my opinion, which are the richer data structure manipulations like groupBy, partition, intersection, difference, union, etc.
Re: Lodash's Chain vs. Native Methods
#20In the article when comparing the native vs lodash implementation the native one is given as: persons .map(p => ({ ...p, age: p.age + 1 })) .filter(p => p.age >= 30) .slice(0, 5); ^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e. person.filter(p => p.age >= 29) .slice(0,5) .map(p => ({...p, age: p.age + 1})
It's a slightly contrived example, but it demonstrates a real situation that can come up. Suppose instead it was: const drinkingAge = 21 persons .map(p => ({ ...p, canDrink: p.age >= drinkingAge })) .filter(p => p.canDrink) .slice(0, 5); Assuming we want `canDrink` to exist in those objects for later use, reversing the steps here would mean duplicating the logic and the work of computing it across the map() and the f…