Search code examples
javascriptarraysfor-loopreversing

Reverse into new array VS reverse in place


I have been trying to reverse an array, first by using push and creating a new array, and then second by using destructuring and mutating the original array.

I'm wondering which of the two runs faster, and why?


Solution

  • Assuming your code is optimized:

    • The second option has a better space complexity (1: O(n^2), 2: O(n+1)).
    • The second option has a better time complexity (1: O(n), 2: O(n/2)).

    Also note that may use the built-in Array.prototype.reverse() method as well.