DEV Community

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

Collapse
 
liaowow profile image
Annie Liao

Thanks Mike for pointing out the potential costly operation of shift -- I read somewhere that also compared shift with push and pop, but your explanation makes more sense to me. And yes, using splice first and adding the specific items to the array via spread operator does look more efficient. Really appreciate your help! :)

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Yeah we can do it without spread - but now it looks nicer in ES6 :)

    function rotateLeft(array, n) {
      n = n % array.length
      return array.push.apply(array, array.splice(0, n))
    }