const { map, find, keyBy } = require('lodash');
function createItems() {
const items = [];
for (let i = 0; i < 5000; i += 1) {
const item = {
id: 1,
name: `Item ${i}`
};
items.push(item);
}
return items;
}
function createOnhands() {
const items = [];
for (let i = 0; i < 5000; i += 1) {
const item = {
id: 1,
cost: 1
};
items.push(item);
}
return items;
}
const items = createItems();
const onhands = createOnhands();
function findItemOnhand() {
console.time('find');
map(items, (item) => {
const itemOnhand = find(onhands, { id: item.id });
return {
id: item.id,
name: item.name,
cost: itemOnhand ? itemOnhand.cost : 0
};
});
console.timeEnd('find');
}
function keyByItemOnhand() {
console.time('keyBy');
const onhand = keyBy(onhands, 'id');
map(items, (item) => {
const itemOnhand = onhand[item.id];
return {
id: item.id,
name: item.name,
cost: itemOnhand ? itemOnhand.cost : 0
};
});
console.timeEnd('keyBy');
}
findItemOnhand();
keyByItemOnhand();