Test

Benchmark created on


Preparation HTML

	

Setup

const items = [
  { id: 1, category: 'fruit', name: 'apple' },
  { id: 2, category: 'vegetable', name: 'carrot' },
  { id: 3, category: 'fruit', name: 'banana' },
  { id: 4, category: 'vegetable', name: 'lettuce' },
  { id: 5, category: 'fruit', name: 'pear' },
  { id: 6, category: 'grain', name: 'rice' },
];

Test runner

Ready to run.

Testing in
TestOps/sec
GroupBy
const groupByObject = () => {
  return Object.groupBy(items, (item) => item.category);
};

groupByObject();
ready
For loop
const groupByForLoop = () => {
  const grouped = {};
  for (const item of items) {
    const category = item.category;
    if (!grouped[category]) {
      grouped[category] = [];
    }
    grouped[category].push(item);
  }
  return grouped;
};

groupByForLoop();
ready
Reduce
const groupByReduce = () => {
  return items.reduce((grouped, item) => {
    const category = item.category;
    if (!grouped[category]) {
      grouped[category] = [];
    }
    grouped[category].push(item);
    return grouped;
  }, {});
};

groupByReduce()
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.