I did not see any formulation of what these methods really do from the point of view of operational complexity, which helped me with conceptualization: quintessence, I think that it is called a βshiftβ, because in fact it should shift all n elements in your array to the new indexes to correctly update the length property.
push () and pop () use more complex operational complexity. No matter how many n values ββin your array or your array.length, push or pop will always perform 1 operation. It does not need to deal with indexes, it does not require iteration, it only needs to perform one operation, always at the end of the stack, either adding or removing the value and index.
Most importantly, note when using push / pop that the other elements in the array are not affected - they are the same values ββin the same metrics of your array. The length of the array is also automatically updated correctly, what do you expect when deleting or adding values.
On the other hand, shift () and unshift () not only add or remove, but should also actually "shift" all the other elements in your array by different indices. This is more complicated and takes longer because the number of operations depends on n, the number of elements in your array or array.length. For each n + 1 more, he must do 1 more operation to transfer each of the values ββto the correct index, having correctly edited the length.
Otherwise, if he did not perform n operations after shift () and moved other elements, you will not have an element with index 0, and it will not change the length of your array, will it? We want the length of our arrays to be updated intuitively, and shift and unshift must do more operations to do this.
mrmaclean89
source share