Why is pop faster than shift?

前端 未结 6 1743
情深已故
情深已故 2020-12-01 11:46

Douglas Crockford, in JavaScript: The Good Parts, states that \"shift is usually much slower than pop\". jsPerf confirms this. Does anyone know why this is the case

6条回答
  •  囚心锁ツ
    2020-12-01 12:35

    To remove the returned item without re-addressing the array and invalidating all references to it, shift() requires moving the entire array around; pop() can simply subtract 1 from its length.

提交回复
热议问题