Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active June 24, 2021 14:41
Show Gist options
  • Save MauricioRobayo/2716c78ea1f9cefb3463ee82a35f728e to your computer and use it in GitHub Desktop.
Save MauricioRobayo/2716c78ea1f9cefb3463ee82a35f728e to your computer and use it in GitHub Desktop.
All code using array.reduce should be rewritten without array.reduce so it's readable by humans https://twitter.com/jaffathecake/status/1213408744362692608 #gogofast
Object transformations should not use reduce.
Creating an object once, then passing the same object back through reduce is using reduce for decoration only. The only part of reduce it's using is the looping.
The applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.
// Good
arr.reduce((total, item) => total + item);
// reduce used only as decoration
const obj = arr.reduce((obj, item) => {
obj[item] = null;
return obj;
}, {});
const obj = Object.fromEntries(
arr.map(item => [item, null]),
);
const obj = {};
for (const item of arr) obj[item] = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment