This algorithm does not modify the original array. The very first line is:

// Create a sortable array to return.
const sortedArray = [ ...unsortedArray ];

By mutating the copy, sortedArray, the original array remains unmutated.

This choice is significantly more performant than an alternative that does not perform any mutations. The latter option would require much more memory, as each split into “sort all values less than pivot” and “sort all values greater than pivot” would double the memory consumption. Your Big O for memory consumption would be astronomical.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store