Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36
Chrome 115
Windows
Desktop
one year ago
Test name Executions per second
Immutable reducer via spread operator 117180.9 Ops/sec
Mutable reducer via property assign 1996495.6 Ops/sec
Script Preparation code:
AخA
 
var arr = [
  { key: 'one',       val: 1 },
  { key: 'two',       val: 2 },
  { key: 'three',     val: 3 },
  { key: 'four',      val: 4 },
  { key: 'five',      val: 5 },
  { key: 'six',       val: 6 },
  { key: 'seven',     val: 7 },
  { key: 'eight',     val: 8 },
  { key: 'nine',      val: 9 },
  { key: 'ten',       val: 10 },
  { key: 'eleven',    val: 11 },
  { key: 'twelve',    val: 12 },
  { key: 'thirteen',  val: 13 },
  { key: 'fourteen',  val: 14 },
  { key: 'fifteen',   val: 15 },
  { key: 'sixteen',   val: 16 },
  { key: 'seventeen', val: 17 },
  { key: 'eighteen',  val: 18 },
  { key: 'nineteen',  val: 19 },
  { key: 'twenty',    val: 20 }
];
Tests:
  • Immutable reducer via spread operator

     
    const reducer = (obj, pair) => 
      ({...obj, [pair.key]: pair.val });
    arr.reduce(reducer, {});
  • Mutable reducer via property assign

     
    const reducer = (obj, pair) => {
      obj[pair.key] = pair.val;
      return obj;
    };
    arr.reduce(reducer, {});