DEV Community

Discussion on: Two Ways to Rotate an Array in JavaScript

Collapse
 
protium profile image
protium

One thing I wonder about space complexity.
You have this

[arr[start], arr[end]] = [arr[end], arr[start]];
Enter fullscreen mode Exit fullscreen mode

which is transpiled to

  var _ref = [arr[end], arr[start]];
  arr[start] = _ref[0];
  arr[end] = _ref[1];
Enter fullscreen mode Exit fullscreen mode

which is allocating space for a 2 items array N times (worst case). Surely, the browser engine would optimise the code but it would be better to create a helper array and reuse. Also, since the helper function is also an object, should we consider that as memory allocation?