DEV Community

Discussion on: Is JavaScript's .shift() Method a Performance Boost?

vallerious profile image

Hello, Annie!
It is not objective to compare how slice is used between the two solutions.
In your solution, you are copying almost the whole array (n-1) in every iteration. In the 10/10 solution the array is copied so that there are no side effects on the input parameter.
The shift operation is operating on the array, not copying, so there is your performance difference.

liaowow profile image
Annie Liao Author

Ah, I see. Thanks for the crisp explainer, Valeri!