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.