Skip to content

[HOLD for payment 2024-06-13] [LOW] [Performance] Add no-spread-in-reduce to avoid recreating array in reduce method #42414

Description

@mountiny

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 OwnerCurrent Issue Owner: @garrettmknight

Activity

  1. self-assigned this
    on May 21, 2024
  2. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on May 21, 2024
  3. melvin-bot commented on May 21, 2024

    @melvin-bot

    Triggered auto assignment to @garrettmknight (NewFeature), see https://stackoverflowteams.com/c/expensify/questions/14418#:~:text=BugZero%20process%20steps%20for%20feature%20requests for more details. Please add this Feature request to a GH project, as outlined in the SO.

  4. melvin-bot commented on May 21, 2024

    @melvin-bot
  5. changed the title [-][LOW] [Performance] Add no-spread-in-reduce to avoid recreating array in reduce method[/-] [+][$250] [LOW] [Performance] Add no-spread-in-reduce to avoid recreating array in reduce method[/+] on May 21, 2024
  6. added
    Help WantedApply this label when an issue is open to proposals by contributors
    and removed on May 21, 2024
  7. melvin-bot commented on May 21, 2024

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @DylanDylann (External)

  8. 14 remaining items

  9. added
    Awaiting PaymentAuto-added when associated PR is deployed to production
    and removed on Jun 6, 2024
  10. changed the title [-][LOW] [Performance] Add no-spread-in-reduce to avoid recreating array in reduce method[/-] [+][HOLD for payment 2024-06-13] [LOW] [Performance] Add no-spread-in-reduce to avoid recreating array in reduce method[/+] on Jun 6, 2024
  11. melvin-bot commented on Jun 6, 2024

    @melvin-bot

    Reviewing label has been removed, please complete the "BugZero Checklist".

  12. melvin-bot commented on Jun 6, 2024

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 1.4.79-11 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2024-06-13. 🎊

    For reference, here are some details about the assignees on this issue:

  13. melvin-bot commented on Jun 6, 2024

    @melvin-bot

    BugZero Checklist: The PR adding this new feature has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    • [@DylanDylann] Please propose regression test steps to ensure the new feature will work correctly on production in further releases.
    • [@garrettmknight] Link the GH issue for creating/updating the regression test once above steps have been agreed upon.
  14. garrettmknight commented on Jun 13, 2024

    @garrettmknight
    Contributor

    @DylanDylann you've been paid out - no regression test for this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionDailyKSv2ExternalAdded to denote the issue can be worked on by a contributorNewFeatureSomething to build that is a new item.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions