0

Here is the code snippet where my filterList is original list contains 300 records data and newList contains the selected list values which an 165 as example as below for loop causes an performance issue

  for(var i=0;i<filterList.length;i++)  
                        {   
                            var count=false;    
                            for(var j=0;j<newList.length;j++)   
                            {   
                                    
                                if(filterList[i].Id==newList[j].Id) 
                                {   
                                    count=true; 
                                    break;  
                                    //filterList.splice(i,1);   
                                }   
                            }   
                            if(count==true) 
                            {   
                                console.log('list displayed');
                                newfilterList.push(filterList[i]);      
                            }   
                                
                        }
                        filterList=newfilterList
                        component.set("v.filterMemberList", filterList);
            var memberList = [];
            

1 Answer 1

1

By using Set above code can be optimized. Complexity will be reduced from 300*165 to 300+165 lines for checking, if filterdList element id matches with newList element id.

var newList = response.getReturnValue();
var newfilterList = [];
let newListIdSet = new Set();
for (var j = 0; j < newList.length; j++) {
    newListIdSet.add(newList[j].Id);
}

for (var i = 0; i < filterList.length; i++) {

    if (newListIdSet.has(filterList[i].Id)) {
        newfilterList.push(filterList[i]);
    }
}

filterList = newfilterList
component.set("v.filterMemberList", filterList); 

With ecmascript this code can be again reduced.

const newList = response.getReturnValue();
let newListIdSet = new Set();

newList.forEach(element=> newListIdSet.add(element.Id));

const newfilterList = filterList.filter(element=> newListIdSet.has(element.Id));

component.set("v.filterMemberList", newfilterList); 
6
  • Even I have tried this approach before but it takes more time and not getting the correct result with this approach .
    – vijay
    May 11, 2022 at 11:26
  • Can you post your code with this approach, that will be helpful to understand the problem May 11, 2022 at 11:43
  • Loading time also depends upon DOM as well, please post cmp file as well. May 11, 2022 at 11:47
  • Hi Amit I have updated the cmp and js file in the code snippet. I am thinking that even in cmp currentpagerecord for pagination is also cause and issue because of multipel iteration of renders.
    – vijay
    May 11, 2022 at 12:23
  • On above approach using set as well its getting the same performance issue
    – vijay
    May 12, 2022 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .