Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  89] [ 7]  / answers: 1 / hits: 30973  / 9 Years ago, thu, july 16, 2015, 12:00:00

If I have a an array of objects called filteredList and a function such as :



function    buildList(filteredList, p1, p2, p3) {
var resultList = [];

for (var i =0; i < filteredList.length; i++) {
if (filteredList[i].type === 'global' && filteredList[i].p1 === p1 && filteredList[i].p2 === p2 && filteredList[i].p3 === p3)
resultList.push(filteredList[i]);
}

return resultList;
}


What would be the performance difference if instead of looping through my array like I do, I would do something like : filteredList.filter(rebuildList)



rebuildList being a function checking the same conditions than buildList



Would it do the same ? (Looping through each element)



Can you think of a more optimized and efficient way to do it ? I'm calling a function like buildList many times in my project and it consumes a great amount of time.


More From » performance

 Answers
32

You can use the Array.prototype.filter method. Example coming soon



Regarding performance you should read here: Fastest way to find an item in a JavaScript Array


[#65787] Tuesday, July 14, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;