HTML Preparation code:
AخA
 
1
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
x
 
var key = 'key';
var object = {
  'a': {
    'key': 'a',
    'foo': 123123,
    'bar': 123,
    'fobar': 456
  }
};
var array = [
  {
    'key': 'b',
    'foo': 67563,
    'bar': 6345,
    'fobar': 3425
  },
  {
    'key': 'c',
    'foo': 34532,
    'bar': 123412,
    'fobar': 534532
  },
  {
    'key': 'd',
    'foo': 1234321,
    'bar': 435234,
    'fobar': 346457
  },
  {
    'key': 'e',
    'foo': 23523,
    'bar': 124325,
    'fobar': 2134235
  },
  {
    'key': 'f',
    'foo': 1235213,
    'bar': 346346,
    'fobar': 213423
  }
];
Tests:
  • Array.prototype.reduce()

     
    array.reduce(function(acc, cur) {
      acc[cur[key]] = cur;
      return acc;
    }, Object.assign({}, object));
  • _.reduce()

     
    _.reduce(array, function(acc, cur) {
      acc[cur[key]] = cur;
      return acc;
    }, Object.assign({}, object));
  • _.reduce() with _.get()

     
    _.reduce(array, function(acc, cur) {
      acc[_.get(cur, key)] = cur;
      return acc;
    }, Object.assign({}, object));
  • _.keyBy()

     
    Object.assign({}, object, _.keyBy(array, key));
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Array.prototype.reduce()
    _.reduce()
    _.reduce() with _.get()
    _.keyBy()

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Chrome 121 on Windows
View result in a separate tab
Test name Executions per second
Array.prototype.reduce() 1190398.5 Ops/sec
_.reduce() 1075433.4 Ops/sec
_.reduce() with _.get() 512104.6 Ops/sec
_.keyBy() 1224186.9 Ops/sec