js in the array
Deduplication Array
Commonly used methods, and the deduplication efficiency Array Analysis:
First we build an array is mainly used for de-duplication test, the magnitude of our main experiment is 1000,10000,100000,500000. Specific method of generating an array of the following:
function buildDataArr (size) {
var array = [];
for (var i =0; i< size; i++) {
var num = Math.round(Math.random() * size);
array.push(num);
}
return array;
}
var arr1 = buildDataArr(1000);
var arr2 = buildDataArr(10000);
var arr3 = buildDataArr(100000);
var arr4 = buildDataArr(500000);
Performed by using a de-emphasis filter index and methods
function distinct(numberArr) { return numberArr.filter((item, index)=> { return numberArr.indexOf(item) === index }) }
By using an array of dual circulation to be heavy
function distinctFor(numberArr) { for (let i=0, len=numberArr.length; i<len; i++) { for (let j=i+1; j<len; j++) { if (numberArr[i] == numberArr[j]) { numberArr.splice(j, 1); // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一 len--; j--; } } } return numberArr }
... of weight and by using de include for
function distinctInclude(numberArr) { let result = [] for (let i of numberArr) { !result.includes(i) && result.push(i) } return result }
Use the sort to be heavy
function distinctSort(numberArr) { numberArr = numberArr.sort() let result = [numberArr[0]] for (let i=1, len=numberArr.length; i<len; i++) { numberArr[i] !== numberArr[i-1] && result.push(numberArr[i]) } return result }
Use new Set de re
function distinctSet(numberArr) { return Array.from(new Set(numberArr)) }
Use heavy objects to
function distinctObj(numberArr) { let result = [] let obj = {} for (var i of numberArr) { if (!obj[i]) { result.push(i) obj[i] = 1 } } return result }
Well, the above method of introduction is over, let's look at the performance:
1000 | 10000 | 100000 | 500000 | |
---|---|---|---|---|
distinctFilter | 0 | 40 | 3992 | 105859 |
distinctFor | 2 | 59 | 5873 | 147234 |
distinctInclude | 1 | 24 | 2320 | 54794 |
distinctSort | 1 | 8 | 69 | 264 |
distinctSet | 0 | 1 | 12 | 78 |
distinctObj | 1 | 1 | 9 | 33 |