Javascript實現數組去重方法及效率對比

前言

其實網上搜索這些方法一堆堆的,之因此還來寫一遍主要是由於本身習慣一種以後就忘了其餘方法怎麼實現,就寫一寫總結一下,順便作個測試看看哪一個效率最高,爲了更好展現效果,我會先總結認爲比較好的方法,後面統一測試。(舒適提示:下文衹是爲了簡便,通常狀況下不建議寫在原型上,容易污染全局)數組

一,經過尋找對象屬性

var ary = [14, 12, 2, 2, 2, 5, 32, 2, 59, 5, 6, 33, 12, 32, 6];
function sortFun1(ary) {
  var obj = {},
    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(sortFun1(ary));
// [ 2, 2, 2, 2, 5, 5, 6, 6, 12, 12, 14, 32, 32, 33, 59 ]

二,經過尋找數組位置

var ary = [14, 12, 2, 2, 2, 5, 32, 2, 59, 5, 6, 33, 12, 32, 6];
function sortFun2(ary) {
  var 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(sortFun2(ary));
// [ 2, 2, 2, 2, 5, 5, 6, 6, 12, 12, 14, 32, 32, 33, 59 ]

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

var ary = [14, 12, 2, 2, 2, 5, 32, 2, 59, 5, 6, 33, 12, 32, 6];
function sortFun3(ary) {
  var 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(sortFun3(ary));
// [ 2, 2, 2, 2, 5, 5, 6, 6, 12, 12, 14, 32, 32, 33, 59 ]

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

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

接下來是網上比較少的效率比較了,也是本文重點,咱們先生成一個隨機數組函數和計時函數統一比較
(舒適提示:隨機數組執行一次以後就保存在一個變量,而每次執行函數花費時間都會有幾毫秒誤差,大家想看多幾種結果就狠狠地按按F5就行了,也只能這麼搞..)瀏覽器

首先生成一個一千個數字的數組dom

function sortFun1(ary) {
  var obj = {},
    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
  });
}

function sortFun2(ary) {
  var 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
  });
}


function sortFun3(ary) {
  var 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
  });
}


function sortFun4(ary) {
  ary.sort(function (a, b) {
    return a - b
  });
  var 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));
  }
  return ary;
}

function useTime(fn, ary) {
  var start = new Date();
  fn(ary);
  var end = new Date();
  console.log('本次函數運行花了:' + (end - start) + '毫秒');
}


var ary = randomAry(1000000);
useTime(sortFun1, ary);
useTime(sortFun2, ary);
useTime(sortFun3, ary);
useTime(sortFun4, ary);

本次函數運行花了:1毫秒
本次函數運行花了:1毫秒
本次函數運行花了:0毫秒
本次函數運行花了:1毫秒
(嗯,果真現代瀏覽器強大無比,不吐槽舊瀏覽器了)函數

看看十萬個數字測試

function sortFun1(ary) {
  var obj = {},
    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
  });
}

function sortFun2(ary) {
  var 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
  });
}


function sortFun3(ary) {
  var 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
  });
}


function sortFun4(ary) {
  ary.sort(function (a, b) {
    return a - b
  });
  var 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));
  }
  return ary;
}

function useTime(fn, ary) {
  var start = new Date();
  fn(ary);
  var end = new Date();
  console.log('本次函數運行花了:' + (end - start) + '毫秒');
}


var ary = randomAry(100000);
useTime(sortFun1, ary);
useTime(sortFun2, ary);
useTime(sortFun3, ary);
useTime(sortFun4, ary);

如今開始出現明顯差距了
本次函數運行花了:65毫秒
本次函數運行花了:4毫秒
本次函數運行花了:4毫秒
本次函數運行花了:5毫秒this

看看一百萬個數字(數據龐大,開始吃不消了,等個五六秒吧)code

function sortFun1(ary) {
  var obj = {},
    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
  });
}

function sortFun2(ary) {
  var 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
  });
}


function sortFun3(ary) {
  var 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
  });
}


function sortFun4(ary) {
  ary.sort(function (a, b) {
    return a - b
  });
  var 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));
  }
  return ary;
}

function useTime(fn, ary) {
  var start = new Date();
  fn(ary);
  var end = new Date();
  console.log('本次函數運行花了:' + (end - start) + '毫秒');
}


var ary = randomAry(1000000);
useTime(sortFun1, ary);
useTime(sortFun2, ary);
useTime(sortFun3, ary);
useTime(sortFun4, ary);

本次函數運行花了:661毫秒
本次函數運行花了:24毫秒
本次函數運行花了:20毫秒
本次函數運行花了:27毫秒對象

總結

數聽說話,排序

相關文章
相關標籤/搜索