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
<div class="fixed-header">
<form>
<div class="form-group">
<input type="text" ng-model="searchText" class="form-control" placeholder="Suche..."/>
</div>
</form>
</div>
<ul>
<li class="initial"
ng-repeat-start="(customer, projectGroup) in projects | searchProjects:searchText">
{{customer}}
</li>
</ul>
Here's the filter:
.filter('searchProjects', ($rootScope, $filter) => {
return (projects, query) => {
if (projects) {
var passFilter = projects.filter(p => {
let backUpContacts = false;
if (p) {
//If project has customer and customer has references, don't search in all of them
if (p.kunde && 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 => p.reference === contact.id);
}
//Search also for project leaders that have to be found first
let projectLeaders = [];
$rootScope.profiles.forEach(profile => {
profile.projects.forEach(role => {
if (role.project === p.id) {
let projectRole = role.masterData['Projektrolle'];
if (projectRole && ~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;
}
};
})