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 }
];
const reducer = (obj, pair) =>
({obj, [pair.key]: pair.val });
arr.reduce(reducer, {});
const reducer = (obj, pair) => {
obj[pair.key] = pair.val;
return Object.assign({}, obj);
};
arr.reduce(reducer, {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Immutable reducer via spread operator | |
Mutable reducer via property assign |
Test name | Executions per second |
---|---|
Immutable reducer via spread operator | 24450.1 Ops/sec |
Mutable reducer via property assign | 20241.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to reduce an array of objects using the reduce()
method:
{...obj, [pair.key]: pair.val}
) to create a new object with the updated properties.obj[pair.key] = pair.val
) to update the existing object.Pros and Cons
Library Used
There is no specific library used in this benchmark. The reduce()
method is a built-in JavaScript method that is supported by most modern browsers and Node.js environments.
Special JS Feature or Syntax
The benchmark uses the spread operator ({...obj, [pair.key]: pair.val}
) which is a feature introduced in ECMAScript 2018 (ES2018). This syntax allows for more concise object creation and merging. The Object.assign()
method used in the second approach is also a built-in JavaScript method that has been around since ES5.
Other Considerations
When working with large datasets or performance-critical code, it's essential to consider the trade-offs between immutability and mutability. In general:
Alternatives
If you're looking for alternative approaches to reduce an array of objects, consider the following options:
reduce()
method: Lodash is a popular utility library that provides a robust implementation of the reduce()
method.mapReduce()
function: Ramda is another functional programming library that provides a concise implementation of the mapReduce()
function, which can be used to reduce an array of objects.forEach()
and Object.assign()
: You can also implement the reduction logic using forEach()
and Object.assign()
methods, although this approach may be less efficient than using the built-in reduce()
method.In summary, the benchmark is testing two approaches to reduce an array of objects using the reduce()
method. The immutable reducer via spread operator provides better predictability but incurs a performance overhead, while the mutable reducer via property assign is faster but may lead to unexpected side effects if not handled carefully.