Underscore flatten vs Array.flat() vs reduce/concat

Benchmark created on


Preparation HTML

<script type="module">
const {flatten} = await import ("https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js");

window.flatten = flatten;
</script>

Setup

const arr1 = [0, 1, 2, [3, 4]];

const reduceFlatten = (arr) =>  arr.reduce((flat, next) => flat.concat(next), []);

Test runner

Ready to run.

Testing in
TestOps/sec
Underscore flatten
flatten(arr1);
ready
Array.flat()
arr1.flat();
ready
Reduce flatten
reduceFlatten(arr1);
ready

Revisions

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