Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Chrome 97
Mac OS X 10.15.7
Desktop
3 years ago
Test name Executions per second
with spread operator 805785.3 Ops/sec
with mutation 2987229.0 Ops/sec
with object assign 156310.5 Ops/sec
with new object assign 70344.8 Ops/sec
foreach 2743494.0 Ops/sec
Script Preparation code:
AخA
 
var range = (from, to) => {
    const output = []
    for (var x = from; x < to; x++) {
        output.push(x)
    }
    return output
}
Tests:
  • with spread operator

     
    range(0, 10).reduce((acc, num) => {
      return {
        ...acc,
        [num]: num
      }
    }, {})
  • with mutation

     
    range(0, 10).reduce((acc, num) => {
      acc[num] = num
      return acc
    }, {})
  • with object assign

     
    range(0, 10).reduce((acc, num) => {
      return Object.assign(acc, {[num]: num})
    }, {})
  • with new object assign

     
    range(0, 10).reduce((acc, num) => {
      return Object.assign({}, acc, {[num]: num})
    }, {})
  • foreach

     
    const output = {};
    range(0, 10).forEach((num) => {
      output[num] = num;
    });