DEV Community

Discussion on: Two Ways to Rotate an Array in JavaScript

Collapse
protium profile image
Brian Mayo

One thing I wonder about space complexity.
You have this

[arr[start], arr[end]] = [arr[end], arr[start]];

which is transpiled to

  var _ref = [arr[end], arr[start]];
  arr[start] = _ref[0];
  arr[end] = _ref[1];

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?