lodash groupBy vs Array.reduce group by 100k
Date tested:
one month ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Test name
Executions per second
Lodash
264.4 Ops/sec
Native
187.1 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 max2 = 100000; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
Lodash
_.groupBy(data, 'id')
Native
data.reduce((acc, item) => { if (!acc[item.id]) { acc[item.id] = []; } acc[item.id].push(item); return acc; }, {})
Open this result on MeasureThat.net