Flattening nested number arrays

Benchmark created on


Setup

const nums = [1, 2, 3, [4, 5, 6, [7, [1, 2, 3], 8], 9], 10, [11], [[[12, [13]]]],[4, 5, 6, [7, [1, 2, 3], 8], 9]]

Test runner

Ready to run.

Testing in
TestOps/sec
recursive function with .flat()
function flatList(items) {
  return items.map((i) => (Array.isArray(i) ? flatList(i).flat() : i)).flat();
}

flatList(nums);
ready
recursive function with .flatMap()
function flatList(items) {
  return items.flatMap((i) => (Array.isArray(i) ? flatList(i).flat() : i));
}

flatList(nums);
ready

Revisions

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