代码之家  ›  专栏  ›  技术社区  ›  Maryam Koulaei

如何优化计算速度数组.forEach在节点.js

  •  2
  • Maryam Koulaei  · 技术社区  · 7 年前

     /**
         * The aggregation data structure:
         * "_id": {
         * "geometry": geometry,
         * "dups": [
         *    "5b3b25b4e54029249c459bfc", keep only the fisrt element in allDocs
         *    "5b3b25b4e54029249c459e65", delete it from allDocs
         *    "5b3b25b4e54029249c459d7d"   delete it from allDocs
         *   ],
         * "dupsProp": [  ], array of all properties of duplicatePoints
         * "count": 3
         */
    var aggregationRes =[46,000 objects]
    var allDocs =[345,000 objects]
    aggregationRes.forEach(function (resElem, counter) {
            console.log(counter + "/" + aggregationRes.length)
            //Delete objects in allDocs based on dups array except the first one
            var foundIndex = allDocs.findIndex(x => x._id.toString() == resElem.dups[0]);
                    //assign the mergedProperties
            allDocs[foundIndex].properties = resElem.dupsProp;
            //delete the remaining ids in Docs from dups array 
            resElem.dups.forEach(function (dupElem, index) {
                var tmpFoundIndex = allDocs.findIndex(x => x._id.toString() == resElem.dups[index + 1]);
                if (tmpFoundIndex !== -1) {
                    allDocs.splice(tmpFoundIndex, 1)
                }
            })
        })

    这个脚本运行了将近4个小时。如您所见,计算非常简单,但由于allDocs数组很大,因此需要相当长的时间。如果有人给我一个如何减少计算时间的提示,那就太好了。 提前谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   juvian    7 年前

    var allDocs =[345,000 objects]
    var aggregationRes =[46,000 objects]
    var allDocsIndexed = {};
    
    allDocs.forEach(function(doc){
        allDocsIndexed[doc._id.toString()] = doc;
    });
    
    aggregationRes.forEach(function (resElem, counter) {
        allDocsIndexed[resElem.dups[0]].properties = resElem.dupsProp;
        for (var i = 1; i < resElem.dupsProp.length; i++) {
            delete allDocsIndexed[resElem.dupsProp[i]];
        }
    });
    
    var allUndeletedDocs = allDocs.filter(doc => allDocsIndexed.hasOwnProperty(doc_id.toString()));