<!--your preparation HTML code goes here-->
var arr = Array(10_000).fill(1)
arr.reduce((acc, x) => {
acc.push(x);
return acc;
}, []);
arr.reduce((acc, x, index) => {
acc[index] = x;
return acc;
}, new Array(arr.length));
arr.reduce((acc, x, index) => {
acc[index] = x;
return acc;
}, new Array(arr.length).fill(null));
arr.reduce((acc, x, index) => {
acc[index] = x;
return acc;
}, new Array());
const a = new Array(arr.length);
arr.reduce((acc, x, index) => {
acc[index] = x;
return acc;
}, a);
const a = [];
arr.reduce((acc, x, index) => {
acc.push(x)
return acc;
}, a);
const a = [];
arr.reduce((acc, x, index) => {
acc[index] = x;
return acc;
}, a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Reduce with empty erray | |
Reduce with filled array | |
Reduce with filled array with null | |
Reduce with new Array | |
Reduce with var | |
Reduce with var [] | |
Reduce with var [] assign by index |
Test name | Executions per second |
---|---|
Reduce with empty erray | 10601.3 Ops/sec |
Reduce with filled array | 5860.2 Ops/sec |
Reduce with filled array with null | 6316.3 Ops/sec |
Reduce with new Array | 5628.4 Ops/sec |
Reduce with var | 21483.9 Ops/sec |
Reduce with var [] | 14137.6 Ops/sec |
Reduce with var [] assign by index | 3608.6 Ops/sec |
The benchmark titled "Reduce array4" evaluates the performance of JavaScript's Array.prototype.reduce()
method across different implementations and initializations of the accumulator (the acc
parameter). The objective is to compare how different styles of using the reduce
method and initializing the accumulator array affect execution speed.
Reduce with empty array:
arr.reduce((acc, x) => { acc.push(x); return acc; }, []);
reduce
method with an empty array and pushes each element into it.push
might be slightly less efficient than assigning values by index as it may involve dynamic resizing of the array.Reduce with filled array:
arr.reduce((acc, x, index) => { acc[index] = x; return acc; }, new Array(arr.length));
acc
with a new array of the same length as arr
, assigning elements by index.Reduce with filled array with null:
arr.reduce((acc, x, index) => { acc[index] = x; return acc; }, new Array(arr.length).fill(null));
null
.null
doesn't add inherent advantages unless they are utilized later in some way.Reduce with new Array:
arr.reduce((acc, x, index) => { acc[index] = x; return acc; }, new Array());
Reduce with var:
const a = new Array(arr.length);
arr.reduce((acc, x, index) => { acc[index] = x; return acc; }, a);
Reduce with var []:
const a = [];
arr.reduce((acc, x, index) => { acc.push(x); return acc; }, a);
reduce
progresses.Reduce with var [] assign by index:
const a = [];
arr.reduce((acc, x, index) => { acc[index] = x; return acc; }, a);
push
method’s overhead.The results, measured as ExecutionsPerSecond
, demonstrate performance variations among the different implementations:
map
could be considered if the order of processing isn't essential, though it won’t directly reduce the array.for
loop may provide a performance benefit in some scenarios, especially if optimized for specific data types or structures.In summary, the benchmark provides insights into various methodologies for using the reduce function in JavaScript, highlighting practical considerations for performance that software engineers should weigh based on the specific requirements of their applications.