var arr = [
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
{
loanId: 1077579,
missingDocs: [
{ docTypeID: 4, docType: "CEMA" },
{ docTypeID: 14, docType: "Policy" },
],
},
];
arr.reduce((acc, loan) => acc + loan.missingDocs.length, 0)
arr.flatMap(loan => loan.missingDocs).length
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce with concat | |
flatMap |
Test name | Executions per second |
---|---|
reduce with concat | 8880006.0 Ops/sec |
flatMap | 2469770.5 Ops/sec |
Benchmark Explanation
The provided JSON represents two benchmark tests: "flatMap count vs reduce count". The benchmarks are designed to compare the performance of two different approaches for processing an array of objects.
Options being compared
flatMap()
method, which returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.reduce()
method, which applies a function against an accumulator and each element in the array (from left-to-right) to reduce it to a single value.Pros and Cons of each approach
reduce
due to the recursive nature of concatenation.flatMap
, as it avoids unnecessary recursive calls.Library usage
In both test cases, no external library is required. The flatMap()
method is a built-in array method in JavaScript, while the reduce()
method is also built-in.
Special JS feature or syntax
There are no special features or syntax used in these benchmarks. They only utilize standard JavaScript methods and data structures.
Other alternatives
If you wanted to compare other approaches for processing arrays, some possible alternatives could include:
for
or while
) instead of flatMap()
or reduce()
.reduce()
.However, for this specific benchmark, the built-in flatMap()
and reduce()
methods are likely to be the most efficient and convenient options.