Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
group-by
Group arrays by object key
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser:
Firefox 128
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
groupBy with two while
3933783.2 Ops/sec
groupBy with while and filter
2348533.0 Ops/sec
groupBy with Set and filter
563889.0 Ops/sec
groupBy with two filters
1463303.8 Ops/sec
Script Preparation code:
var collection = [ { name: 'Fabver', type: 1, company: 'ADL', }, { name: 'Fabian', type: 0, company: 'ADL', }, { name: 'Andrés', type: 0, company: 'JWT', }, { name: 'Andrew', type: 1, company: 'JWT', }, ]; var index = Math.floor(Math.random() * 3); var key = ['name', 'type', 'company'][index]
Tests:
groupBy with two while
if (!collection.length) return [collection]; const origin = [...collection]; const groups = []; while (origin.length) { const group = []; const obj = origin.shift(); if (obj) { group.push(obj); let i = 0; while (i < origin.length) { if (obj[key] === origin[i][key]) { group.push(origin.splice(i, 1)[0]); } i++; } } groups.push(group); } return groups;
groupBy with while and filter
if (!collection.length) return [collection]; const groups = []; let i = 0; let selected = {}; while (i < collection.length) { if (!selected || selected[key] !== collection[i][key]) { selected = collection[i]; groups.push(collection.filter((item) => item[key] === selected[key])); } i++; } return groups;
groupBy with Set and filter
if (!collection.length) return [collection]; const groups = []; const keyValues = collection.map((item) => item[key]); const uniqueValues = [...new Set(keyValues)]; uniqueValues.forEach((value) => groups.push(collection.filter((item) => item[key] === value))); return groups;
groupBy with two filters
if (!collection.length) return [collection]; const groups = []; const keyValues = collection.map((item) => item[key]); const uniqueValues = keyValues.filter((value, index, self) => self.indexOf(value) === index); uniqueValues.forEach((value) => groups.push(collection.filter((item) => item[key] === value))); return groups;