flatten-vs-flat

Benchmark created on


Setup

let array = [0];

for (let i = 1; i < 100; i++) {
	array = [i, array];
}

Test runner

Ready to run.

Testing in
TestOps/sec
flat
const result = array.flat();
ready
flatten
const flattenArray = (array) =>
  array.reduce((acc, item) => {
    if (Array.isArray(item)) {
      return [...acc, ...flattenArray(item)]
    }
    return [...acc, item]
  }, [])
 
const result = flattenArray(array);
ready

Revisions

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