DEV Community

Discussion on: Flattening an Array, a Performance Test

Collapse
 
rhymes profile image
rhymes

Can you test with the spread operator too?

function flatten(array) {
  return [].concat(...array)
}

And also using Array.prototype.flat ?

array.flat()

This last one is not available in all browsers (no Edge or IE).

Collapse
 
ryan_dunton profile image
Ryan Dunton

I updated the test suite to take in the spread operator and flat() tests, but it didn't seem to be marketly faster than the others, actually flat seems like it is the slowest so far. You can see the results here.

I actually hadn't heard of the flat() function until you mentioned it. I had trouble getting it to work in my local node environment but was able to work it in Chrome. Not sure why.

Collapse
 
rhymes profile image
rhymes

I actually hadn't heard of the flat() function until you mentioned it. I had trouble getting it to work in my local node environment but was able to work it in Chrome. Not sure why.

It's not supported by Node :D

Thanks for the updated info!