0
\$\begingroup\$

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;
        }
    };
})
\$\endgroup\$
0
\$\begingroup\$

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) => {
        return (projects, query) => {
            if (projects) {
                //Create project leader search object
                let projectLeaders = {};
                $rootScope.profiles.forEach(profile => {
                    profile.projects.forEach(role => {
                        let projectRole = role.masterData['Projektrolle'];
                        //864 = Projectleader Tag ID
                        if (projectRole && ~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 => {
                    //delete customers' contacts to exclude from search
                    if (p.kunde && p.kunde.contacts) {
                        //Only leave the selected Reference Contact in Customer's Contacts for Search
                        p.kunde.contacts = p.kunde.contacts.filter(contact => 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);
                });
            }
        };
    })
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.