4 ms·
Exactly. If I need just filter/map/some etc., I use it. But if I need a combination of more than one, that's a job for reduce().
by FlameWolf 16d ago
Exactly. If I need just filter/map/some etc., I use it. But if I need a combination of more than one, that's a job for reduce().
- ggorlen 16d agoSo you prefer arr.reduce((acc, el) => { if (el % 2 === 0) { acc.push(el * 2); } return acc; }, []); over arr.filter(e => e % 2 === 0).map(e => e * 2) The only advantage of the reduction as I see it is performance, but this is highly dubious and would need to be profiled for proof (I don't recall seeing removing a pass like this matter in practice). And if perf does matter, a for..of loop would be clearer and one-pass, not to mention async-compatible: const result = []; for (const el of arr) { if (el % 2 === 0) { result.push(el * 2); } } Exercises like this illustrate why verbal technical job interviews are useful in the age of LLMs--a series of A/B taste preferences seems high signal and ripe for discussion: "Ah, so you're choosing reduce for perf... please describe a scenario you encountered where this made a measurable impact".