lodash vs map 4
Date tested:
2 months ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.56
Test name
Executions per second
Lodash uniqBy path
2191.1 Ops/sec
Lodash uniqBy function
2117.8 Ops/sec
Map
1181.2 Ops/sec
Set and filter
834.1 Ops/sec
map and filter index
432.8 Ops/sec
Benchmark definition (click to collapse):
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var myArr = Array.from({ length: 16000 }, () => ({ value: Math.floor(Math.random() * 1000) })); var myCopy = null;
Tests:
Lodash uniqBy path
myCopy = _.uniqBy(myArr, 'value');
Lodash uniqBy function
myCopy = _.uniqBy(myArr, (obj) => obj.value);
Map
myCopy = [...new Map(myArr.map((obj) => [obj.value, obj])).values()];
Set and filter
const valuesSet = new Set(myArr.map(({ value }) => value)); myCopy = myArr.filter(({ value }) => valuesSet.has(value));
map and filter index
const valuesArr = myArr.map(({ value }) => value); myCopy = myArr.filter(({ value }, index) => valuesArr.indexOf(value) === index);
Open this result on MeasureThat.net