Problem
When you use the spread operator in an array reduce, it often leads to a partial object being copied over and over again in a loop.
// BAD
const reducedObject = myArray.reduce(
(acc, key) => ({
...acc,
[key]: someOtherObject[key],
}),
{},
);
Especially with large objects, this is very inefficient because ...acc will unnecessarily copy the full object on each iteration, rather than just adding the single key we want to add.
Solution
Prohibit this pattern with a new ESLint rule - no-spread-in-reduce. The above example would result in a lint error, and you might fix it with something more simple and efficient, like this:
GOOD
const reducedObject = myArray.reduce(
(acc, key) => {
acc[key] = someOtherObject[key];
return acc;
},
{},
)
@kacper-mikolajczak
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~011102640f041d624c
- Upwork Job ID: 1792878366584131584
- Last Price Increase: 2024-05-21
- Automatic offers:
- DylanDylann | Reviewer | 0
Issue Owner
Current Issue Owner: @garrettmknight
Problem
When you use the spread operator in an array reduce, it often leads to a partial object being copied over and over again in a loop.
Especially with large objects, this is very inefficient because ...acc will unnecessarily copy the full object on each iteration, rather than just adding the single key we want to add.
Solution
Prohibit this pattern with a new ESLint rule - no-spread-in-reduce. The above example would result in a lint error, and you might fix it with something more simple and efficient, like this:
@kacper-mikolajczak
Upwork Automation - Do Not Edit
Issue Owner
Current Issue Owner: @garrettmknight