reduce ??= vs if

Benchmark created on


Setup

const array = Array.from({ length: 1e4 }, (_, i) => ({ id: i % 10 }));

Test runner

Ready to run.

Testing in
TestOps/sec
??=
array.reduce((acc, item) => {
  acc[item.id] ??= [];
  acc[item.id].push(item.id);
  return acc;
}, {});
ready
if
array.reduce((acc, item) => {
  if (!acc[item.id]) acc[item.id] = [];
  acc[item.id].push(item.id);
  return acc;
}, {});
ready

Revisions

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