Deep custom AngularJS Filter I need to filter projects using a searchbar, it should filter all properties of the projects with a few exceptions. It's working, but it's extremely slow, where can I make optimizations?
The "_" is from the library underscore.js

&lt;div class="fixed-header"&gt;
    &lt;form&gt;
        &lt;div class="form-group"&gt;
            &lt;input type="text" ng-model="searchText" class="form-control" placeholder="Suche..."/&gt;
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/div&gt;

&lt;ul&gt;
    &lt;li class="initial" 
        ng-repeat-start="(customer, projectGroup) in projects | searchProjects:searchText"&gt;
       {{customer}}
    &lt;/li&gt;
&lt;/ul&gt;


Here's the filter:

.filter('searchProjects', ($rootScope, $filter) =&gt; {
    return (projects, query) =&gt; {
        if (projects) {
            var passFilter = projects.filter(p =&gt; {
                let backUpContacts = false;
                if (p) {

                    //If project has customer and customer has references, don't search in all of them
                    if (p.kunde &amp;&amp; p.kunde.contacts) {
                        backUpContacts = p.kunde.contacts.concat([]);
                        //Only leave the selected Reference Contact in Customer's Contacts for Search
                        p.kunde.contacts = p.kunde.contacts.filter(contact =&gt; p.reference === contact.id);
                    }

                    //Search also for project leaders that have to be found first
                    let projectLeaders = [];
                    $rootScope.profiles.forEach(profile =&gt; {
                        profile.projects.forEach(role =&gt; {
                            if (role.project === p.id) {
                                let projectRole = role.masterData['Projektrolle'];
                                if (projectRole &amp;&amp; ~projectRole.indexOf(864)) {
                                    projectLeaders.push(profile.firstName + " " + profile.lastName);
                                }
                            }
                        });
                    });
                    p["projectLeaders"] = projectLeaders;
                }
                let pContain = _.contains($filter('filter')(projects, query), p);
                delete p["projectLeaders"];
                if (backUpContacts) {
                    p.kunde.contacts = backUpContacts;
                }
                return pContain;
            });
            return passFilter;
        }
    };
})
I tried to improve it myself and it is much faster than before!
Here's my new code, feel free to comment on what can be further done to improve it.  

    .filter('searchProjects', ($rootScope, $filter) =&gt; {
        return (projects, query) =&gt; {
            if (projects) {
                //Create project leader search object
                let projectLeaders = {};
                $rootScope.profiles.forEach(profile =&gt; {
                    profile.projects.forEach(role =&gt; {
                        let projectRole = role.masterData['Projektrolle'];
                        //864 = Projectleader Tag ID
                        if (projectRole &amp;&amp; ~projectRole.indexOf(864)) {
                            let projectLeader = profile.firstName + " " + profile.lastName;
                            if (projectLeaders.hasOwnProperty(role.project)) {
                                projectLeaders[role.project].push(projectLeader);
                            } else {
                                projectLeaders[role.project] = [projectLeader];
                            }
                        }
                    });
                });

                return projects.filter(p =&gt; {
                    //delete customers' contacts to exclude from search
                    if (p.kunde &amp;&amp; p.kunde.contacts) {
                        //Only leave the selected Reference Contact in Customer's Contacts for Search
                        p.kunde.contacts = p.kunde.contacts.filter(contact =&gt; p.reference === contact.id);
                    }
                    //if project has leaders, add them to it for searching
                    if (projectLeaders.hasOwnProperty(p.id)) {
                        p["projectLeaders"] = projectLeaders[p.id];
                    }
                    return Boolean($filter('filter')([p], query).length);
                });
            }
        };
    })