flatMap vs reduce

Benchmark created on


Setup

const submissionInput = [
  {
  	documentIds: [1,2,3,4,5,6,7]
  },
  {
  	documentIds: [1,2,3,4,5,6,7]
  },
  {
  	documentIds: [1,2,3,4,5,6,7]
  }
]

Test runner

Ready to run.

Testing in
TestOps/sec
flatMap
const totalDocumentsUploaded = submissionInput.flatMap(
  (requests) => requests.documentIds,
).length;

console.log(totalDocumentsUploaded)
ready
reduce
const totalDocumentsUploaded = submissionInput.reduce(
  (accumulator, requestStep) => accumulator + requestStep.documentIds.length,
0);

console.log(totalDocumentsUploaded);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.