Flat Mapping

Benchmark created on


Setup

test = [...Array(100_000)].map(e=>~~(Math.random()*10)) 
test.map(num => num % 2 === 1 ? [2, 2] : 1).flat()
const fn = num => num % 2 === 1 ? [2, 2] : 1

Test runner

Ready to run.

Testing in
TestOps/sec
map/flat
result = test.map(fn).flat()
ready
flatMap
result = test.flatMap(fn)
ready
forLoop
const result = []
for (let i = 0; i < test.length; i++) {
  const immer = fn(test[i])
  if (Array.isArray(immer)) {
    for(let j = 0; j < immer.length; j++) {
      result[result.length] = immer[j]
    }
  } else {
    result[result.length] = immer
  }
}
ready

Revisions

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