Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Prevent spread ...obj inside a arr.reduce()  #18

Closed

Description

@styfle

I've seen this pattern used several times where the developer is using spread inside a reduce causing a O(N^2) algorithm when avoiding the reduce would lead to O(N).

Example usage we want to avoid:

O(N^2)

  const originalFiles = {}; // Imagine 100k files

  const remappedFiles = Object.keys(originalFiles).reduce(
    (mappedFiles, file) => ({
      ...mappedFiles,
      [`_next/${file}`]: originalFiles[file]
    }),
    {}
  );

Could we write a lint rule to prevent this pattern and guide the developer toward the O(N) solution?

Example usage we should encourage:

O(N)

  const originalFiles = {}; // Imagine 100k files
  const remappedFiles = {};
  for (const file of Object.keys(originalFiles)) {
    remappedFiles[`_next/${file}`] = originalFiles[file];
  }

Activity

  1. added
    enhancement
    on May 17, 2022
  2. mrmckeb commented on Jun 10, 2022

    @mrmckeb
    Contributor

    This is quite interesting, have you seen any lint rules around this that we could turn on?

  3. mrmckeb commented on Sep 21, 2022

    @mrmckeb
    Contributor

    @styfle did you have any thoughts around how we might introduce a rule like this?

    Obviously this would need some discussion, as a lot of JS engineers like functional programming.

  4. styfle commented on Sep 21, 2022

    @styfle
    MemberAuthor

    Obviously this would need some discussion, as a lot of JS engineers like functional programming.

    I think there is little to discuss when we consider performance.

    In fact, @pvdz just found a case where changing the spread in the loop brought the build time down from 18 min to 4 minutes.

    Also see The reduce ({...spread}) anti-pattern blog post.

  5. styfle commented on Sep 21, 2022

    @styfle
    MemberAuthor

    did you have any thoughts around how we might introduce a rule like this?

    I assume we need to write a eslint plugin. Something like this:

    https://github.com/mkosir/eslint-plugin-no-array-reduce/blob/63b7792d1bbf3f8d3578fdb6fbde5982555f12c0/src/rules/disallowArrayMethod.ts#L11-L24

    Or we could disable .reduce() completely with no-array-reduce, but that might upset some engineers as you mentioned.

  6. AlexErrant commented on Apr 7, 2023

    @AlexErrant

    I googled "javascript spread reduce eslint" after reading an article on Tanstack Table and arrived here. I think a lot of devs (including myself) don't think about the implications of .... I'm not smart enough to help write a rule; just here to express interest :)

  7. Conaclos commented on Dec 4, 2023

    @Conaclos

    We added this rule to Biome several months ago. Any feedbacks are welcomed :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementeslint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions