Flat Mapping (v3)

Revision 3 of this 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
for of
const result = []
for (const value of test) {
  const immer = fn(value)
  	
  if (Array.isArray(immer)) {
  	for (const child of immer) {
  		result[result.length] = child
  	}
  } else {
  	result[result.length] = value
  }
}
ready

Revisions

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