Home > Net >  Optimize code in JavaScript (Currently using too many for loops)
Optimize code in JavaScript (Currently using too many for loops)

Time:12-05

I have been working on a data formating question, and finally got the answer.

However, my answer has too many loops, and it will slow down time complexity when implementing.

Below is my code:

const userInfo = [];

for (let i = 0; i < projects.length; i  ) {
  const userProject = projects[i].userProjects;

  for (let j = 0; j < userProject.length; j  ) {
    const userExist = userInfo.some((item) =>
      item.userId.includes(userProject[j].user.userId)
    );
    if (!userExist) userInfo.push(userProject[j].user);
  }
}

userInfo.forEach((user) => {
  let tempArr = [];
  for (let i = 0; i < projects.length; i  ) {
    const userProject = projects[i].userProjects;
    for (let j = 0; j < userProject.length; j  ) {
      if (user.userId === userProject[j].userId)
        tempArr.push({
          projectId: projects[i].projectId,
          name: projects[i].name,
        });
    }
  }
  user.project = tempArr;
});

console.log(userInfo);

Is there a way I can optimize my code? I tried to combine the nested for loops together but not working...

Please help. Thank you so much!!!

CodePudding user response:

In the top part, the outer two loops look necessary (since you have N projects * M userProjects), but the third .some isn't - change the userInfo from an array to a mapping of userIds to their users, so inside the second loop, you only have to look up to see if the userId already exists (O(1)), and assign if it doesn't.

Instead of assigning the user, assign an object of the shape you need at the end - a [{ projectId, name }]. This'll save you from having to go through the whole array again - once you've checked to see if you need to create the surrounding array for the userId, you can push the object to the array whether or not you had to create the array on that iteration or not.

const userProjectsByUserId = new Map();
for (const { userProjects, projectId, name } of projects) {
    for (const { user } of projects) {
        if (!userProjectsByUserId.has(user.userId)) {
            userProjectsByUserId.set(user.userId, { user, projects: [] }]);
        }
        userProjectsByUserId.get(user.userId).projects.push({ projectId, name });
    }
}

This'll give you a Map of the shape

userId => {
  user,
  projects: [
    { projectId, name },
    ...

If you need exact data structure your current code is giving you, then afterwards, do:

const output = [...userProjectsByUserId.values()]
  .map(({ user, projects }) => ({ ...user, project: projects }));

(though you might consider using projects instead of project, since it's a collection of projects, not a single project)

  • Related