Ravi
Ravi

Reputation: 69

Optimise JavaScript

Can someone please help me to find best way to optimise the below code as it is taking a long time when I have thousand of records searching

var arr =[
   {
      children:[
         {
            children:[
               {
                  children:[
                     {
                        name:'XYZZZZZ'
                     }
                  ]
               }
            ]
         }
      ]
   }
];

        let list = [];

        //Calculate column list
        arr.forEach((obj0) => {
            if (obj0.hasOwnProperty('children')) {
                if (obj0.children.length > 0) {
                    let objchid1 = obj0.children;
                    objchid1.forEach((obj1) => {
                        if (obj1.hasOwnProperty('children')) {
                            if (obj1.children.length > 0) {
                                let objchid2 = obj1.children;
                                objchid2.forEach((obj2) => {
                                    if (obj2.hasOwnProperty('children')) {
                                        if (obj2.children.length > 0) {
                                            let objchid3 = obj2.children;
                                         objchid3.forEach((obj3) => {
                                            if (obj3.name !== 'james') {
                                    console.log('IN THREEE', obj3.name);
                                                list.push(obj3.name);
                                                }
                                            });

                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

I have tried searching a lot but no luck Thanks in advance.!!!

Upvotes: 0

Views: 83

Answers (2)

Slai
Slai

Reputation: 22876

If the data is from a JSON string, the search can be done during the parsing :

var list = [], json = '[{"child":[{"child":[{"child":[{"name":"XYZZZZZ"}]}]}]}]'

var arr = JSON.parse(json, (key, val) => (key === 'name' && list.push(val), val))

console.log(list)
console.log(arr)

Upvotes: 0

Phil
Phil

Reputation: 7576

  1. Optimize your data structure. Do not use nested arrays unless you really need to. NoSQL is so popular when it comes to WebDev because reads happen 100.000 times more than writes and saving on bandwidth (for you and the user) is worth more than saving on duplicate data in a database considering how cheap hardware is
  2. You can save the elements of the deepest array as object keys (with the nested .name attribute in your case) and the index of the respective position in the array as the object value. This way you can do myArray[myElementsToIndexObject['elementIamLookingFor']] iterating only one single time over the nested array (for building myElementsToIndexObject)

Upvotes: 2

Related Questions