Javasscript Learning Notes Array Learn all the methods of the two arrays

Source: Internet
Author: User

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 undefinedThe 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.