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 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));
  • _.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()
    _.keyBy()

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2025 years ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
Chrome 64 on Mac OS X 10.13.3
View result in a separate tab
Test name Executions per second
Array.prototype.reduce() 1131684.4 Ops/sec
_.reduce() 900060.5 Ops/sec
_.keyBy() 474935.3 Ops/sec