HTML Preparation code:
AخA
 
1
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
x
 
var arr = [
  {
   id: 123,
   name: "abc",
  },
  {
   id: 1234,
   name: "abcd",
  },
  {
   id: 123,
   name: "abc",
  },
  {
    id: 4321,
    name: 'dcba',
  },
];
var key='id';
Tests:
  • Pure JS: map

     
    var indexArray = [];
    var newArray = arr.map(a => { if (indexArray.indexOf(a[key]) < 0) { indexArray.push(a[key]); return a; };  return -1; }).filter(a => a != -1);
  • Pure JS: forEach

     
    var indexArray = [];
    var newArray = [];
    arr.forEach(a => { if (indexArray.indexOf(a[key]) < 0) { indexArray.push(a[key]); newArray.push(a); }});
  • Lodash: uniqBy

     
    var newArray = _.uniqBy(arr, key);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Pure JS: map
    Pure JS: forEach
    Lodash: uniqBy

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Chrome 130 on Linux
View result in a separate tab
Test name Executions per second
Pure JS: map 885628.6 Ops/sec
Pure JS: forEach 1108264.6 Ops/sec
Lodash: uniqBy 2463656.2 Ops/sec