function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alexander AtkinsonAlexander Atkinson 

Javascript: "Array.find()" is really slow. How can I speed it up or is there a better alternative?

So I have some code that is really inefficient. It has an array of objects all created from an apex query. When I move somebody to a new location, the component will fire an event with the moved persons information stored. This is then used in a "find" method for my array of objects to find the object inside of the array to edit and update so the component will update visually.

The problem is this line of code becomes extremely inefficient when passing 200 objects.

Sample:
 
var titleid = event.getParam("titleid");
        var title = event.getParam("title");
        var maxSeats = event.getParam("maxSeats");
        var seatsInUse = event.getParam("seatsInUse");  
        var item = event.getParam("item"); //This is the item that was moved.
        var allGuestsList = component.get("v.allItems"); //This is the array of objects
        
        //Search guest list for matching guest to edit the array so the component will update graphically.
        var actualGuest = allGuestsList.find(function(moving){return moving.id == item.id;}); //***REALLY INEFFICIENT***

 
Raj VakatiRaj Vakati
Can you use Array.prototype.findIndex(predicate, thisValue) and see .. 


Array.prototype.includes also work
 
Raj VakatiRaj Vakati
give me complete code to see the issue 
Alexander AtkinsonAlexander Atkinson
I changed the code for each item to already have its index stored as an extra variable and ready to access in the array. However the real problem now appears to be the component.set(). It all works the same. The component setter causes lag though, presumably from having to pass a large array of objects into the lightning component.
 
var titleid = event.getParam("titleid");
        var title = event.getParam("title");
        var maxSeats = event.getParam("maxSeats");
        var seatsInUse = event.getParam("seatsInUse");  
        var item = event.getParam("item"); //This is the item that was moved.
        var allGuestsList = component.get("v.allItems");
        console.log(item.index);
       
            //Has the guest been moved to another table?
            if(item.status != title)
            {  
                //Does table have seats spare?
                if(seatsInUse>=maxSeats)
                {
                    console.log(title, 'is full!\nMaximum guests for this table:', maxSeats);
                }
                else
                {
                    item.status = title;
                    item.statusid = titleid;
                    
					allGuestsList[item.index] = item;
                    component.set("v.allItems", allGuestsList); //This Line Is the new cause of lag