数组值只包含了字符和数字,更多范例增添不会影响以下method_*的排序(时候排序)
测试环境:版本 57.0.2987.133 (64-bit)
var arr1 = [1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 1, 2, 3, 4, 5, 6, "1", "2", "1", "2"];
var arr2 = [1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 1, 2, 3, 4, 5, 6];
while(arr1.length < 600000){
arr1 = arr1.concat(arr1);
arr2 = arr2.concat(arr2);
}
//
//
//
// method_1:新建数组-双轮回-对照-push
Array.prototype.unique = function(){
var res = [this[0]],
len = this.length;
for(var i = 1; i < len; i ++){
var repeat = false,
rlen = res.length;
for (var j = 0; j < rlen; j ++){
if(this[i] === res[j]){
repeat = true;
break;
}
}
!repeat && res.push(this[i]);
}
return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 18
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 10
//
//
//
// method_2:新建数组-排序-单轮回-对照-push
Array.prototype.unique = function () {
this.sort(function (a, b) {
if (typeof a != typeof b && a == b) {
if (typeof a == "number") {
return -1;
} else {
return 1
}
}
return parseInt(a) - parseInt(b);
})
var res = [this[0]],
len = this.length;
for (var i = 1; i < len; i ++) {
var slen = res.length;
if (this[i] !== res[slen -1]) {
res.push(this[i]);
}
}
return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 121
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 93
//
//
//
// method_3:新建数组-新建对象-单轮回-对象对照-数组push
Array.prototype.unique = function () {
var res = [],
obj = {},
len = this.length;
for (var i = 0; i < len; i++) {
// 因为对象属性为字符串 1 和 "1" 雷同,因而做不到 ===
!obj[this[i]] && res.push(this[i]) && (obj[this[i]] = 1);
}
return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 8
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 5
//
//
// method_4:新建数组-轮回-向后查询-数组push
Array.prototype.unique = function () {
var res = [],
len = this.length,
i, j;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
if (this[i] === this[j]) {
j = false;
break;
}
}
j && res.push(this[i]);
}
return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 28
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 17
//
//
//
// method_4:运用Set
Array.prototype.unique = function (){
var arr = new Set(this);
return [...arr];
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 75
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 60
//
//
//
// method_5:运用find/findIndex/indexOf
Array.prototype.unique = function(){
var res = [this[0]],
len = this.length;
for(var i = 1; i < len; i ++){
var repeat = false,
rlen = res.length;
if(!res.find((v,k)=>{return v === this[i]})){
res.push(this[i]);
}
// if(res.indexOf(this[i]) == -1){
// res.push(this[i]);
// }
// if(res.findIndex((v,k)=>{return v === this[i]}) == -1){
// res.push(this[i]);
// }
}
return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 均匀 110+
var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 均匀 65+
总结
method_1时候为 18 /10
,轮回对照。
method_2时候为 121/93
, 假如依靠sort
排序(在字符和数字组合中更是依靠sort callback
)大大增添的时候,而且影响了原有数组递次。
method_3时候为 8/5
, 没法辨别字符和数字。
method_4时候为 28/7
,同method_1为轮回对照,然则增添了内层轮回次数。
method_5时候为 75/60
, 运用Set数据结构特征。
method_5时候为 110+/65+
, 运用find/findIndex/indexOf推断。
整体来讲最好挑选为 method_1
然则引荐运用method_5—-大道至简