Home | Mailing List | Blog | Tutorial Videos

Array-to-Object Efficiency

Both a and b are objects with the same properties and values. Which is created more efficiently?

const arr = [1, 2, 3];

const a = arr.reduce(
  (acc, el, i) => ({ ...acc, [el]: i }),
  {}
);

const b = {};
for (let i = 0; i < arr.length; i++) {
  b[arr[i]] = i;
}

Select one: