The following is my online three kinds of efficient methods of summary and efficiency testing, if you have better opinions or suggestions can also be proposed, we encourage learning.
The array goes to the weight Method 1:
Array.prototype.unique1 = function () {
console.time ("Array to Weight Method 1"); The time the record starts executing
var arr = []; Creates a temporary array of
var obj = {}; Creates an empty object
for (var i = 0; i < this.length i++) {//traversal of the currently going to heavy array
if (!obj[this[i]]) { //To determine if the Obj object contains the current item, not execute
Arr.push (This[i]); Push current item to temporary array
obj[this[i] = 1; Save the current item in obj Object
}
}
console.timeend ("Array to Weight Method 1"); The time the record ends execution return
arr;
}
The array goes to the weight Method 2:
Array.prototype.unique2 = function () {
console.time ("Array to Weight Method 2");//record time of start execution
var arr = []; Creates a temporary array
for (var i = 0; i < this.length i++) { //traversal of the currently going to heavy array
if (Arr.indexof (this[i)) = = 1) { // Determines whether the current item is present in the temporary array and does not execute
arr.push (this[i]);///Push the current item to a temporary array
}
console.timeend ("Array to redo 2"); The time the record ends execution return
arr;
}
The array goes to the weight Method 3:
Array.prototype.unique3 = function () {
console.time ("Array to Weight Method 3");//records Start execution time
var arr = [This[0]];//create a temporary array, And will go to the first item of the heavy array into the temporary array for
(var i = 1; i < this.length; i++) {//from going to the heavy array the second entry begins traversing the
if (This.indexof (this[i)) = = i) {//Judge Pro Whether the current item is present in the array, not executing
arr.push (this[i]);///Push the current item to the temporary array
}
console.timeend ("Array to Redo Method 3"); The time the record ends execution return
arr;
}
Efficiency test Method:
var arr1 = []; Create an array to go heavy for
(var i = 0; i < 200000 i++) {//Traverse 200,000 data
Arr1.push (parseint (Math.random () * 10) + 1);// There is data returned as a number of random numbers (between 1-10) and pushed into the array to be
Console.log}
(Arr1.unique1 ());//print array to redo the execution time of the method 1
console.log ( Arr1.unique2 ()); Print array to 2 execution time
Console.log (Arr1.unique3 ());//print array go to Weight Method 3 execution Time
Efficiency test Results:
Summarize
The above is about several kinds of JavaScript array to weight method efficiency test, hope that the results of the test for everyone to choose to use an array to weight method can help, if you have questions you can message exchange. Thank you for your support to the cloud-dwelling community.