Test name | Executions per second |
---|---|
Lodash uniqBy | 3437.6 Ops/sec |
Custom uniq array | 823.6 Ops/sec |
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
var myArr = Array.from({
length: 16000
}, () => ({ value: Math.floor(Math.random() * 1000) }));
var myCopy = null;
myCopy = _.uniqBy(myArr, 'value');
const itemIdSet = new Set(myArr.map(item => item.value));
const uniqueItems = [];
itemIdSet.forEach(value => {
const uniqueItem = myArr.find(item => item.value === value);
if (uniqueItem) uniqueItems.push(uniqueItem);
});
myCopy = uniqueItems;