最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter performance on objects array - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Jul 16, 2015 at 11:14 ElloneEllone 3,89812 gold badges47 silver badges77 bronze badges 1
  • What is filteredList? It is a array or object. It is an object then performance will be fasted. – SK. Commented Jul 16, 2015 at 11:45
Add a ment  | 

1 Answer 1

Reset to default 9

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

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

发布评论

评论列表(0)

  1. 暂无评论