Methods for 1.push () and pop () stacks LIFO
Push () This method adds one or more elements to the end of the array and returns the new length .
push()
Method can receive any number of arguments, add them to the end of the array one by one, and return the length of the modified array
The POP () method is just the opposite of the push()
method. pop()
method to delete the last element of the array, subtract the length of the array, 1
and return the value of the deleted element , if the array becomes empty, the method does not change the array, the return undefine
value
Unshift () add element and Shift () method to delete element queue FIFO first
unshift()
The method is to add one or more elements to the beginning of the array and return the new length.
shift()
Methods and
unshift()
Method is the opposite. This method is used toremoves the first element of the array from it and returns the deleted value。 If the array is empty,
shift()
Method will not take any action and return
undefined
The value
Simple to recall:
push()
Method can add one or more elements to the end of an array
shift()
method to remove the first element in the array
unshift()
method to add one or more elements to the front of the array
pop()
method to delete the last element in the array
Performance testing of Push and unshift
Array push()
and unshift()
methods can be added to the current array element, the difference is that it is push()
added at the end, but unshift()
it is added at the beginning, from the principle can be known, unshift()
the efficiency is low. The reason is that every element it adds, it moves the existing element down one position. But how effective is the difference? Here's a quick test.
Here are the results of chrome running
Visible, unshift()
much push()
slower than it 100
is! Therefore, it is usually prudent to use unshift()
, especially for large arrays. That if the effect must be achieved unshift()
, you can use the method of array reverse()
, the method of array reverse()
can be inverted an array. First, the element to be put into the array is push()
added, then reverse()
executed once, unshift()
the effect is achieved
And it takes very little time to perform very high.
Summarize
Javasscript Learning Notes Array Learn all the methods of the two arrays