当前位置:首页 >> 网络编程

js实现数组去重方法及效率對比

其實網上搜索這些方法一堆堆的,之所以還來寫一遍主要是因爲自己習慣一種之後就忘了其他方法怎麽實現,就寫一寫總結一下,順便做個測試看看哪個效率最高,爲了更好展示效果,我會先總結認爲比較好的方法,後面統一測試。(溫馨提示:下文衹是爲了簡便,一般情況下不建議寫在原型上,容易污染全局)

一,通過尋找對象屬性

var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique1 = function(){
 var obj = {},
  ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(!obj[this[i]]){
   obj[this[i]] = 1;
   ary.push(this[i]);
  }
 }
 return ary.sort(function(a,b){return a - b});
}
console.log(n.unique1());

二,通過尋找數組位置

var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique2 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
console.log(n.unique2());

三,跟尋找數組位置類似,搜索數字第一次出現的位置是不是跟當前位置一樣

var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique3 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this.indexOf(this[i]) == i) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
console.log(n.unique3());

四,比較常規,先排序,再比較前後兩個數字是不是相等

var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique4 = function(){
 this.sort(function(a,b){return a - b});
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this[i] !== this[i-1]) ary.push(this[i]);
 }
 return ary;
}
console.log(n.unique4());

接下來是網上比較少的效率比較了,也是本文重點,我們先生成一個計時函數統一比較

首先生成一個一百數字的數組

Array.prototype.unique1 = function(){
 var obj = {},
  ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(!obj[this[i]]){
   obj[this[i]] = 1;
   ary.push(this[i]);
  }
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this.indexOf(this[i]) == i) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
 this.sort(function(a,b){return a - b});
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this[i] !== this[i-1]) ary.push(this[i]);
 }
 return ary;
}
function randomAry (n) {
 var ary = [],i=0;
 for(; i<n; i++){
  ary.push(Math.ceil(Math.random()*10000));
 }
  console.log(ary)
 return ary;
}
function useTime (fn) {
 var start = new Date();
 fn();
 var end = new Date();
 console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(100),
 fn1 = function(){
 ary.unique1()
 },
 fn2 = function(){
 ary.unique2()
 },
 fn3 = function(){
 ary.unique3()
 },
 fn4 = function(){
 ary.unique4()
 };
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);

結果在我的谷歌瀏覽器發現

方法一:0毫秒

方法二:1毫秒

方法三:0毫秒

方法四:00毫秒

(嗯,果然現代瀏覽器强大無比,不吐槽舊瀏覽器了)

看看1000個數字

Array.prototype.unique1 = function(){
 var obj = {},
  ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(!obj[this[i]]){
   obj[this[i]] = 1;
   ary.push(this[i]);
  }
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this.indexOf(this[i]) == i) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
 this.sort(function(a,b){return a - b});
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this[i] !== this[i-1]) ary.push(this[i]);
 }
 return ary;
}
function randomAry (n) {
 var ary = [],i=0;
 for(; i<n; i++){
  ary.push(Math.ceil(Math.random()*10000));
 }
  console.log(ary)
 return ary;
}
function useTime (fn) {
 var start = new Date();
 fn();
 var end = new Date();
 console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(1000),
 fn1 = function(){
 ary.unique1()
 },
 fn2 = function(){
 ary.unique2()
 },
 fn3 = function(){
 ary.unique3()
 },
 fn4 = function(){
 ary.unique4()
 };
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
方法一:1-2毫秒
方法二:40-50毫秒
方法三:40-50毫秒
方法四:0-1毫秒
看看10000個數字(數據龐大,開始吃不消了,等個五六秒吧)
Array.prototype.unique1 = function(){
 var obj = {},
  ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(!obj[this[i]]){
   obj[this[i]] = 1;
   ary.push(this[i]);
  }
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this.indexOf(this[i]) == i) ary.push(this[i]);
 }
 return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
 this.sort(function(a,b){return a - b});
 var ary = [],
  i= 0,
  len = this.length;
 for(; i<len; i++){
  if(this[i] !== this[i-1]) ary.push(this[i]);
 }
 return ary;
}
function randomAry (n) {
 var ary = [],i=0;
 for(; i<n; i++){
  ary.push(Math.ceil(Math.random()*10000));
 }
  console.log(ary)
 return ary;
}
function useTime (fn) {
 var start = new Date();
 fn();
 var end = new Date();
 console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(10000),
 fn1 = function(){
 ary.unique1()
 },
 fn2 = function(){
 ary.unique2()
 },
 fn3 = function(){
 ary.unique3()
 },
 fn4 = function(){
 ary.unique4()
 };
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);

因爲數據龐大,我衹拿一次數據,你們喜歡就刷新幾次比較唄

方法一:10毫秒

方法二:1258毫秒

方法三:2972毫秒

方法四:5毫秒

原本還想給更大的數據看看,然後十萬個我的頁面就奔潰了。。。潰了。。。了

然後試試五萬個,也奔潰了。。。潰了。。。了

那就算吧,

反正在一萬個數據裏面對比,數據越大,方法一跟方法四按毫秒遞增,方法二跟方法三效率感人

效率計算:4>1>2>3

方法計算: 先排序,再比較前後兩個數字是不是相等 > 通過尋找對象屬性 > 通過尋找數組位置 > 搜索數字第一次出現的位置是不是跟當前位置一樣

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!