Hacker News new | ask | show | jobs
by latexr 64 days ago
The arguments for “process everything” don’t pass mustard, you’re not comparing the same thing.

> Chaining nudges you toward “process everything,” even when that’s not what you meant to do.

  const firstActiveUser = users
    .filter(user => user.active)
    .map(user => user.name)[0];
> This filters the entire array, maps the result, and then grabs one item.

> When what you actually wanted was:

  const user = users.find(user => user.active);
  const name = user?.name;
Then if that’s what you wanted, do that!

  const name = users.find(user => user.active)?.name
The fact that you processed everything in the first example was entirely your choice, it has nothing to do with chaining.