Array Flattening

In the image above, both operations are identical.

Yes.

Internally, when you try call the toString() method on an Array, it simply delegates the job to the Array's native join() method, which then recursively calls join() & hence, flattens the array out, giving you a finely printed flattened array.

However, unlike the new flat() method, it doesn't return an Array. It simply returns you a string.

About Efficiency

You may ask, whether this approach is efficient?
I’d say — Well, it is not inefficient.

Internally, the flat() method does a couple of more checks than the join() method but none of them add any overhead of time complexity.

Plus and the join() method is a very generic one.

This would also mean that flat() will be a more reliable function if you want to do some serious operations.

Originally published at http://bigfatsoftwareinc.wordpress.com on April 13, 2021.

--

--