Performance Comparison of Three array replication methods in javascript

Source: Internet
Author: User

I. Three Methods for copying Arrays
1. by slice
Var arr = [1, 2, 3], copyArr;
CopyArr = arr. slice ();

2. by concat
Var arr = [1, 2, 3], copyArr;
CopyArr = arr. concat ();
3. by loop
Var arr = [1, 2, 3], copyArr = [];
For (var I = 0, j = arr. length; I
Ii. Test Environment
Browser: IE6 +, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3

Iii. Test Cases
Use the above three methods to copy an array with 500000 items, and then compare the time consumed by the three methods.
<! DOCTYPE html> <pead> <meta http-equiv = "Content-Type" content = "text/html; charset = GB18030 "/> <title> performance comparison of the three array replication methods </title> <meta name =" description "content =" array replication, slice, concat "/> <meta name =" keywords "content =" array replication, slice, concat "/> </pead> <body> <p> we use an array with 500000 sub-items as the test object (IE won't crash !). </P> <button id = "J_CopyBySlice"> copyBySlice </button> <button id = "J_CopyByConcat"> copyByConcat </button> <button id = "J_CopyByLoop"> copyByLoop </button> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

4. Test results (click the picture to view the big picture)

V. Conclusion
For IE, use slice; for non-IE, use concat.
For webkit, use concat; for other browsers, use slice.

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.