如何使用 javascript 中的两个数组列表提高比较和选择对象的性能
How to increase the performance of compare and Pick the objects using Two array-list in javascript
您好,我有两组数组对象,我只需要选择更高的 MRP
在更新的列表中。我能够使用 lodash 地图和查找功能获得结果。 10 到 15 k 条记录需要更多时间。有没有办法提高性能。这是我的代码。
var ExisitingData=[{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 2000,
},
{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}
....15k]
var updatedData=[{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 4000,
},
{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}
....15k]
var newData=[]
_.map(result1, function (item) {
var updateRecord = _.find(data3, {'barcode': item['barcode']});
if (updateRecord) {
if (item['mrp'] > updateRecord['mrp']) {
newData.push(item);
}
}
});
这似乎 运行 快了很多(对于我有 15k 项的人来说几乎是瞬间):
// group the two lists by barcode
const grouped = _.groupBy([...ExisitingData, ...updatedData], 'barcode');
// ignore any barcode that was added or removed
const pairs = _.filter(grouped, pair => pair.length === 2);
// filter by mrp change
const filteredPairs = _.filter(pairs, ([existingRecord, updatedRecord]) => updatedRecord.mrp > existingRecord.mrp);
// map to items
const newData = _.map(filteredPairs, ([existingRecord, updatedRecord]) => updatedRecord);
如果您还想包括已添加的新项目(即,当条形码存在于更新数据列表中但不存在于现有数据中时),您可以像这样包括它们:
const addedItems = _.differenceBy(updatedData, ExisitingData, 'barcode');
const newData2 = [...newData, ...addedItems];
非 ES6 版本(对我来说已经有一段时间了,希望我做对了):
// group the two lists by barcode
var grouped = _.groupBy(ExisitingData.concat(updatedData), 'barcode');
// ignore any barcode that was added or removed
var pairs = _.filter(grouped, function(pair) {
return pair.length === 2;
});
// filter by mrp change
var filteredPairs = _.filter(pairs, function(pair) {
return pair[1].mrp > pair[0].mrp;
});
// map result to item objects
var newData = _.map(filteredPairs, function(pair) {
return pair[1];
});
包括新项目:
var addedItems = _.differenceBy(updatedData, ExisitingData, 'barcode');
var newData2 = newData.concat(addedItems);
您可以使用普通 JavaScript 高效地完成此操作,并且不需要更多代码。我建议使用 ES6 映射通过条形码键入 updatedData,这样您就可以快速查找 existingData 中的每个条形码。在这里,我将该映射作为 this
对象提供给 filter
:
let newData = existingData.filter(function (o) {
return o.mrp > this.get(o.barcode);
}, new Map(updatedData.map( o => [o.barcode, o.mrp] )) );
const existingData = [{
"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 2000,
}, {
"isActive" : true,
"barcode" : "2699001592228",
"mrp" : 1000,
}];
const updatedData = [{
"isActive" : true,
"barcode" : "2699001592228",
"mrp" : 4000,
}, {
"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}];
let newData = existingData.filter(function (o) {
return o.mrp > this.get(o.barcode);
}, new Map(updatedData.map( o => [o.barcode, o.mrp] )) );
console.log(newData);
所以这会迭代第二个列表一次(为了创建地图),并迭代第一个列表一次(为了过滤它)。 Map#get
方法以常数时间运行,因此此代码在 O(n+m) 时间内运行,其中 n 和 m是两个数组的大小。
您好,我有两组数组对象,我只需要选择更高的 MRP 在更新的列表中。我能够使用 lodash 地图和查找功能获得结果。 10 到 15 k 条记录需要更多时间。有没有办法提高性能。这是我的代码。
var ExisitingData=[{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 2000,
},
{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}
....15k]
var updatedData=[{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 4000,
},
{"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}
....15k]
var newData=[]
_.map(result1, function (item) {
var updateRecord = _.find(data3, {'barcode': item['barcode']});
if (updateRecord) {
if (item['mrp'] > updateRecord['mrp']) {
newData.push(item);
}
}
});
这似乎 运行 快了很多(对于我有 15k 项的人来说几乎是瞬间):
// group the two lists by barcode
const grouped = _.groupBy([...ExisitingData, ...updatedData], 'barcode');
// ignore any barcode that was added or removed
const pairs = _.filter(grouped, pair => pair.length === 2);
// filter by mrp change
const filteredPairs = _.filter(pairs, ([existingRecord, updatedRecord]) => updatedRecord.mrp > existingRecord.mrp);
// map to items
const newData = _.map(filteredPairs, ([existingRecord, updatedRecord]) => updatedRecord);
如果您还想包括已添加的新项目(即,当条形码存在于更新数据列表中但不存在于现有数据中时),您可以像这样包括它们:
const addedItems = _.differenceBy(updatedData, ExisitingData, 'barcode');
const newData2 = [...newData, ...addedItems];
非 ES6 版本(对我来说已经有一段时间了,希望我做对了):
// group the two lists by barcode
var grouped = _.groupBy(ExisitingData.concat(updatedData), 'barcode');
// ignore any barcode that was added or removed
var pairs = _.filter(grouped, function(pair) {
return pair.length === 2;
});
// filter by mrp change
var filteredPairs = _.filter(pairs, function(pair) {
return pair[1].mrp > pair[0].mrp;
});
// map result to item objects
var newData = _.map(filteredPairs, function(pair) {
return pair[1];
});
包括新项目:
var addedItems = _.differenceBy(updatedData, ExisitingData, 'barcode');
var newData2 = newData.concat(addedItems);
您可以使用普通 JavaScript 高效地完成此操作,并且不需要更多代码。我建议使用 ES6 映射通过条形码键入 updatedData,这样您就可以快速查找 existingData 中的每个条形码。在这里,我将该映射作为 this
对象提供给 filter
:
let newData = existingData.filter(function (o) {
return o.mrp > this.get(o.barcode);
}, new Map(updatedData.map( o => [o.barcode, o.mrp] )) );
const existingData = [{
"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 2000,
}, {
"isActive" : true,
"barcode" : "2699001592228",
"mrp" : 1000,
}];
const updatedData = [{
"isActive" : true,
"barcode" : "2699001592228",
"mrp" : 4000,
}, {
"isActive" : true,
"barcode" : "8908001921015",
"mrp" : 1000,
}];
let newData = existingData.filter(function (o) {
return o.mrp > this.get(o.barcode);
}, new Map(updatedData.map( o => [o.barcode, o.mrp] )) );
console.log(newData);
所以这会迭代第二个列表一次(为了创建地图),并迭代第一个列表一次(为了过滤它)。 Map#get
方法以常数时间运行,因此此代码在 O(n+m) 时间内运行,其中 n 和 m是两个数组的大小。