I have a massive collection of various objects that I need to sift through.
With over 60,000 elements, the search operation can sometimes be painfully slow.
One typical object in this array has the following structure:
{
"title": "title"
"company": "abc company"
"rating": 13 // internal rating based on comments and interactions
...
}
My aim is to search for items based on their title and company information, while prioritizing them by their respective ratings.
This snippet showcases my current search implementation:
onSearchInput(searchTerm) {
(<any>window).clearTimeout(this.searchInputTimeout);
this.searchInputTimeout = window.setTimeout(() => {
this.searchForFood(searchTerm);
}, 500);
}
searchForFood(searchTerm) {
if (searchTerm.length > 1) {
this.searchResults = [];
this.foodList.map(item => {
searchTerm.split(' ').map(searchTermPart => {
if (item.title.toLowerCase().includes(searchTermPart.toLowerCase())
|| item.company.toLowerCase().includes(searchTermPart.toLowerCase())) {
this.searchResults.push(item);
}
});
});
this.searchResults = this.searchResults.sort(function(a, b) {
return a.rating - b.rating;
}).reverse();
} else {
this.searchResults = [];
}
}
Inquiry: Any suggestions on how to enhance the search algorithm for better performance?