DEV Community

Discussion on: 1 line of code: How to split an Array in half

 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Non-mutating version - slower though:

const splitInHalf = (a,b=[])=>[b=[...a],b.splice(b.length+1>>1)]
Enter fullscreen mode Exit fullscreen mode

Speed comparision of all 3 methods.

Thread Thread
 
martinkr profile image
Martin Krause

I will stick with the original version.
If you find a faster one without mutations we can use it.
Cheers!