Efficiency Test of several methods for removing the JavaScript array, javascript Array

Source: Internet
Author: User

Efficiency Test of several methods for removing the JavaScript array, javascript Array

The following is my summary of the three high-efficiency methods on the Internet and the efficiency test. If you have better comments or suggestions, you can also give them some suggestions.

Array deduplication Method 1:

Array. prototype. unique1 = function () {console. time ("array deduplication 1"); // record execution start time var arr = []; // create a temporary array var obj = {}; // create an empty object for (var I = 0; I <this. length; I ++) {// traverse the array to be duplicated now if (! Obj [this [I]) {// determines whether the current item exists in the obj object. If not, arr is executed. push (this [I]); // push the current item to the temporary array. obj [this [I] = 1; // Save the current item to the obj object} console. timeEnd ("array deduplication 1"); // record the end time of execution return arr ;}

Array deduplication 2:

Array. prototype. unique2 = function () {console. time ("array deduplication 2"); // record execution start time var arr = []; // create a temporary array for (var I = 0; I <this. length; I ++) {// traverse the array to be duplicated now if (arr. indexOf (this [I]) =-1) {// checks whether the current item exists in the temporary array. If not, arr is executed. push (this [I]); // push the current item to a temporary array} console. timeEnd ("array deduplication 2"); // record the end time of execution return arr ;}

Array deduplication 3:

Array. prototype. unique3 = function () {console. time ("array deduplication 3"); // record execution start time var arr = [this [0]; // create a temporary array, and save the first entry of the deduplication array to the temporary array for (var I = 1; I <this. length; I ++) {// traverse if (this. indexOf (this [I]) = I) {// checks whether the current item exists in the temporary array. If not, arr is executed. push (this [I]); // push the current item to a temporary array} console. timeEnd ("array deduplication 3"); // record the end time of execution return arr ;}

Efficiency Test method:

Var arr1 = []; // create an array to be duplicated (var I = 0; I <200000; I ++) {// traverse 200000 data arr1.push (parseInt (Math. random () * 10) + 1); // return all data to the number of random numbers (between 1 and 10) and push it to the array to be duplicated} console. log (arr1.unique1 (); // print the execution time of array deduplication method 1. log (arr1.unique2 (); // print the execution time of array deduplication 2 console. log (arr1.unique3 (); // print the execution time of array deduplication 3

Efficiency Test results:

Summary

The above is about the Efficiency Test of several array deduplication methods in JavaScript. I hope the test results will be helpful for you to choose the array deduplication method. If you have any questions, please leave a message. Thank you for your support.

Related Article

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.