Home >Web Front-end >JS Tutorial >Performance comparison of three array copy methods in javascript_javascript skills

Performance comparison of three array copy methods in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:35:501430browse

1. Three array copy methods
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
2. Test environment
Browsers: IE6, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3

3. Test case
Use the above 3 methods to test an array with 500,000 items Carry out the copy operation, and then compare the time spent by the three methods.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]


4. Test Results (click the image to see a larger image) Performance comparison of three array copy methods in javascript_javascript skills

5. Conclusion For IE, use slice; for non-IE, use concat.
For webkit, use concat ; For other browsers, use slice.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn