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
@styfle

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];
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions