Test name | Executions per second |
---|---|
Lodash (_.uniqBy) | 2.4 Ops/sec |
Vanilla code | 0.8 Ops/sec |
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
const max2 = 10000000; // 10,000,000 (10 Million)
const arr2 = Array.from({ length: 10000000 }, (_, i) => Math.random() >= 0.5 ? ({ id: i }) : ({ id: Math.floor(Math.random() * 10) + 1 }))
_.uniqBy(arr2, (el) => el.id)
const idsAlreadySeen = new Set();
const uniqArr = arr2.filter(data => {
if (idsAlreadySeen.has(data.id)) {
return false;
}
idsAlreadySeen.add(data.id);
return true;
});