With nearly 25, 000 parallel structure data, how can JavaScript have better performance to combine into a tree structure?

I use the following to loop out data for nearly 23 seconds. Is there a better way to optimize < del > Thank you < / del >

export async function getOrgTree(treeArray){
  let r = [];
  let tmpMap ={};
  for(let i = 0, len = treeArray.length; i < len; iPP){   
    tmpMap[treeArray[i]["code"]]= treeArray[i];
  }
  for(let i = 0, len = treeArray.length; i < len; iPP){
    let key = tmpMap[treeArray[i]["parentCode"]];
    
    if (key) {
      if (!key["children"]) {
        key["children"] = [];
        key["children"].push(treeArray[i]);
      } else {
        if (key["children"].findIndex(v => v.code === treeArray[i].code) === -1) {
          key["children"].push(treeArray[i]);
        }
      }
      if(!treeArray[i]["parentCode"]||treeArray[i]["parentCode"]==="0"||treeArray[i]["parentCode"]==="root"){
        r.push(treeArray[i]);
      }
    } else if (!treeArray[i]["parentCode"]||treeArray[i]["parentCode"]==="0"||treeArray[i]["parentCode"]==="root"){
      r.push(treeArray[i]);
    }
  }
  return r;
}
Jul.06,2021

find out why it took so long

if (key['children'].findIndex(v => v.code === treeArray[i].code) === -1) {
  key['children'].push(treeArray[i]);
}

since this sentence is to loop every element in key ['children'] , which we don't need to loop, it is changed to

.
if (key['children'].some(v => v.code !== treeArray[i].code)) {
  key['children'].push(treeArray[i]);
}

whole modification:

export async function getOrgTree(treeArray){
  let r = [];
  let tmpMap ={};
  for(let i = 0, len = treeArray.length; i < len; iPP){
    tmpMap[treeArray[i]['code']]= treeArray[i];
  }
  for(let i = 0, len = treeArray.length; i < len; iPP){
    let key = tmpMap[treeArray[i]['parentCode']];    
    if (key) {
      if (!key['children']) {
        key['children'] = [];
        key['children'].push(treeArray[i]);
      } else {
        if (key['children'].some(v => v.code !== treeArray[i].code)) {
          key['children'].push(treeArray[i]);
        }
      }
    } else{
      r.push(treeArray[i]);
    }
  }
  return r;
}

Loop mode also has performance. for.of is better than for.. IPP be slow. If you test more than 20,000 data in person, it will be slower 1 second .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb6391-5d3d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb6391-5d3d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?