Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Chrome 130
Mac OS X 10.15.7
Desktop
6 months ago
Test name Executions per second
Lodash 68.3 Ops/sec
Native 40.3 Ops/sec
HTML Preparation code:
x
 
1
2
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
 
var max2 = 500000;
var data = [];
for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
  • Lodash

     
    _.groupBy(data, ({ id }) => id)
  • Native

     
    // Using reduce to mimic _.groupBy
    const grouped = data.reduce((acc, item) => {
        // Initialize an array for this id if it doesn't exist
        if (!acc[item.id]) acc[item.id] = [];
        // Push the item into the array for this id
        acc[item.id].push(item);
        return acc;
    }, {});