Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object.groupBy vs manual groupBy object vs manual groupBy Map
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser:
Chrome 141
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
6 months ago
Test name
Executions per second
Object.groupBy
48292.6 Ops/sec
Manual groupBy obj
75669.0 Ops/sec
Manual groupBy map 1
82302.6 Ops/sec
Manual groupBy map 2
71308.7 Ops/sec
Script Preparation code:
function groupByObj(array, mapper) { return array.reduce( (acc, item) => { const key = mapper(item); const items = acc[key] ?? (acc[key] = []); items.push(item); return acc; }, {}, ); } function groupByMap1(array, mapper) { return array.reduce( (acc, item) => { const key = mapper(item); let items = acc.get(key); if (!items) { items = [] acc.set(key, items) } items.push(item); return acc; }, new Map(), ); } function groupByMap2(array, mapper) { return array.reduce( (acc, item) => { const key = mapper(item); if (!acc.has(key)) { acc.set(key, [item]) } else { acc.get(key).push(item) } return acc; }, new Map(), ); } function makeid(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min); } var data = [] for (let i = 0; i < 1000; i++) { data.push({ name: makeid(randomIntFromInterval(5, 16)), age: randomIntFromInterval(1, 100), occupation: Math.random() > 0.8 ? makeid(randomIntFromInterval(5, 16)) : null }) }
Tests:
Object.groupBy
return Object.groupBy(data, item => item.occupation)
Manual groupBy obj
return groupByObj(data, item => item.occupation)
Manual groupBy map 1
return groupByMap1(data, item => item.occupation)
Manual groupBy map 2
return groupByMap2(data, item => item.occupation)