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.