0

I have an if statement inside two nested for loops in my javascript code as below.

The problem is that the execution slows down when I have to compare thousands of objects in the array as the loop iterates through the entire array just to check for a single matching value.

When the key in data object matches with the id of an object from the arr array it adds a username key to the objects in arr array.

Please run the following code and see the output :

var data = { 1: 'John',
  2: 'Josehp',
  8: 'Marley',
  3: 'George',
  4: 'Stella',
  5: 'Stanley',
  123: 'Juhi'
}



var arr = [ { id: '1'
   },
  { id: '2'
  },
  { id: '10'
    },
  { id: '3'
    },
  { id: '4'
    },
  { id: '13'
    },
  { id: '5'
    }
];

var x;
for (x in data) {
	for (i = 0; i < arr.length; i++) {
	if (arr[i].id == x) {
	arr[i].username = data[x];
	}
     }
  }
console.log(arr)

How can I optimise this code to avoid nested loops or simply improve the performance ?

2
  • 1
    if loop? Do you mean if statement? Commented Apr 2, 2019 at 7:13
  • corrected it. thank you Commented Apr 2, 2019 at 7:13

7 Answers 7

1

may be something like this will help?

arr.forEach((user) => {
    if (data[user.id] !== undefined) {
        user.username = data[user.id]
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

really loved this short answer
1

You could take a single loop and check if the object exists and update the array.

var data = { 1: 'John', 2: 'Josehp', 8: 'Marley', 3: 'George', 4: 'Stella', 5: 'Stanley', 123: 'Juhi' },
    array = [ { id: '1' }, { id: '2' }, { id: '10' }, { id: '3' }, { id: '4' }, { id: '13' }, { id: '5' }],
    i;

for (i = 0; i < array.length; i++) {
    if (array[i].id in data) {
        array[i].username = data[array[i].id];
    }
}

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

data is an hash with id as key, so access it's elements by id is an O(1) operation.

This means that you can have a single loop through your arr variable making your algorithm linear (O(n)):

for (let i = 0; i < arr.length; i++ )
  if ( data[arr[i].id] )
    arr[i].username = data[arr[i].id]

Comments

1

You can use Array.prototype.map and ternary.

var data = { 1: 'John',
  2: 'Josehp',
  8: 'Marley',
  3: 'George',
  4: 'Stella',
  5: 'Stanley',
  123: 'Juhi'
}



var arr = [ { id: '1'
   },
  { id: '2'
  },
  { id: '10'
    },
  { id: '3'
    },
  { id: '4'
    },
  { id: '13'
    },
  { id: '5'
    }
];

let out = arr.map(({id}) => (data[id]? {id, username: data[id]}: {id}));
console.log(out)

Comments

1

Just use map with a ternary operator and spreading:

var data = { 1: 'John', 2: 'Josehp', 8: 'Marley', 3: 'George', 4: 'Stella', 5: 'Stanley', 123: 'Juhi'}

var arr = [{id: '1'},{id: '2'},{id: '10'},{id: '3'},{id: '4'},{id: '13'},{id: '5'}];

arr = arr.map(e => data[e.id] ? { ...e, username: data[e.id] } : e);

console.log(arr)
.as-console-wrapper { max-height: 100% !important; top: auto; }

Comments

1

You could just loop through the arr array and set each item's username based on the property of data with the same key.

var data = {
  1: 'John',
  2: 'Josehp',
  8: 'Marley',
  3: 'George',
  4: 'Stella',
  5: 'Stanley',
  123: 'Juhi'
};

var arr = [{ id: '1' },
{ id: '2' },
{ id: '10' },
{ id: '3' },
{ id: '4' },
{ id: '13' },
{ id: '5' }];


arr.forEach(function(item) {
  item.username = data[item.id];
});
console.log(arr);

Comments

1
var data = { 1: 'John', 2: 'Josehp', 8: 'Marley', 3: 'George', 4: 'Stella', 5: 'Stanley', 123: 'Juhi' },
array = [ { id: '1' }, { id: '2' }, { id: '10' }, { id: '3' }, { id: '4' }, { id: '13' }, { id: '5' }],
i;

for (var m in array){
 array[m].username = data[array[m].id]
}

console.log(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.