0

I have three for loop as below to integrate their objects.
The problem is the length of 'cars' array is 20,000.
So it should runs every 20,000 times for finding same id between company.user and cars.
But the id of cars is unique.
Can I reduce this repeat number in JS? I want to reduce the taking time.
Thank you for reading it.

p.s. I uploaded same question adding the concrete logic inside of for loop.

for (let i = 0; i < company.length; i += 1) {
  for (let j = 0; j < company[i].user.length; j += 1) {
    for (let k = 0; k < cars.length; k += 1) {
      if (company[i].user[j].id === cars[k].id) {
        company[i].user[j] = {
          ...company[i].user[j],
          ...cars[k] 
        }
      }
    }
  }
}
Thomas Jason
  • 538
  • 2
  • 7
  • 18

3 Answers3

1

If there is only 1 match then use break after you found that item. Imagine you find that item at index 1 for example, then the loop would still continue and do 19998 loops. Because of the information that you know there is only 1 possible match you can break it there.

    if (company[i].user[j].id === cars[k].id) {
      company[i].user[j] = {
        ...company[i].user[j],
        ...cars[k] 
      }
      break;
    }
bill.gates
  • 14,145
  • 3
  • 19
  • 47
0
 for (let i = 0, leni = company.length;; i < leni ; i += 1) {
    for (let j = 0, lenj = company[i].user.length; j < lenj; j += 1) {
      for (let k = 0, lenk = cars.length; k < lenk; k += 1) {
        if (company[i].user[j].id === cars[k].id) {
          company[i].user[j] = {
            ...company[i].user[j],
            ...cars[k] 
          }
          break; // match only the first one, then stop searching for cars
        }
      }
    }
  }

Answer based on test results from https://jsperf.com/caching-array-length/4

Spread left in base on

https://thecodebarbarian.com/object-assign-vs-object-spread.html

enter image description here

Trevor
  • 2,792
  • 1
  • 30
  • 43
0

this will optimize your code a little more, but ziga1337 is right, the only effective way is to sort your two objects

// company: [ { user: [ { id: '?1' 
// cars: [ { id: '?2' 

for (let iCompagny of compagny) {
  for (let iUser of iCompagny.user) {
    let fCar = cars.find(xCar.id===iUser.id)
    if (fCar) Object.assign(iUser, fCar)
  }
}   

in case there is always a car.id unique to each user.id:

let indexCars = cars.reduce((a,c)=>{ a[c.id]=c; return a },{})

compagny.forEach(iCompagny=>{
  iCompagny.user.forEach(iUser=>{ Object.assign(iUser, indexCars[iUser.id]) })
})
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40