i need to optimize the time complexity of the given code kindly help.it is written in javascript.
for(let i=0;i<userList.length;i++){
let clientValues = [];
for(let j=0;j<userList[i].clients.length;j++){
for(let k=0;k<clientList.length;k++){
if(userList[i].clients[j] === clientList[k].client_id){
clientValues.push(clientList[k].clientName);
}
}
}
userList[i].clients = clientValues;
}
Assuming clientList is reasonably large, one option is to iterate over it only once to create a map that can be used as a constant-time lookup on each iteration of userList and userList[i].clients:
let clientMap = new Map(clientList.map(c => [c.client_id, c.clientName]))
userList.forEach(u => {
u.clients = u.clients
.map(c => clientMap.get(c))
.filter(c => c !== undefined)
})