返回列表

Array的去重方法unique效率对比

默认分类 2017/09/06 01:10

Chrome下测试结果:

unique1: 1188  
unique2: 39 
unique3: 10 
unique4: 30 
unique5: 5

IE下测试结果:

unique1: 1417 
unique2: 24 
unique3: 9 
unique4: 7 
unique5: 4

FF下测试结果:

unique1: 1812 
unique2: 20 
unique3: 15 
unique4: 3 
unique5: 11

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>array-remove-repeate</title>
<style>
.tt {
  background-color: #006699;
  height: 3px;
  overflow: hidden;
}
</style>
</head>
<body>
  <h4>测试结果</h4>
  <div class="result" id="result"></div>
  <script>
  var output = function(str) {
    result.innerHTML += str + '<br/>'
  }

  Array.prototype.unique1 = function() {
    var r = new Array();
    label: for (var i = 0, n = this.length; i < n; i++) {
      for (var x = 0, y = r.length; x < y; x++) {
        if (r[x] == this[i]) {
          continue label;
        }
      }
      r[r.length] = this[i];
    }
    return r;
  }

  Array.prototype.unique2 = function() {
    return this.sort().join(",,").replace(/(,|^)([^,]+)(,,\2)+(,|$)/g, "$1$2$4").replace(/,,+/g, ",").replace(/,$/, "").split(",");
  }

  Array.prototype.unique3 = function() {
    var temp = {},
      len = this.length;
    for (var i = 0; i < len; i++) {
      var tmp = this[i];
      if (!temp.hasOwnProperty(tmp)) {
        temp[this[i]] = "my god";
      }
    }

    len = 0;
    var tempArr = [];
    for (var i in temp) {
      tempArr[len++] = i;
    }
    return tempArr;
  }

  Array.prototype.unique4 = function() {
    var temp = new Array();
    this.sort();
    for (i = 0; i < this.length; i++) {
      if (this[i] == this[i + 1]) {
        continue;
      }
      temp[temp.length] = this[i];
    }
    return temp;
  }

  Array.prototype.unique5 = function() {
    var res = [],
      hash = {};
    for (var i = 0, elem;
      (elem = this[i]) != null; i++) {
      if (!hash[elem]) {
        res.push(elem);
        hash[elem] = true;
      }
    }
    return res;
  }

  var test = (function() {
    var arr2 = [];
    for (var i = 0; i < 20000; i++) {
      var t = i;
      t = parseInt(Math.random() * 20000) + 1;
      arr2[i] = (t.toString());
    }
    return function() {
      return arr2;
    };
  })();
  window.onload = function() {
    var aa01, aa02, aa03, aa04, aa05;
    var bb01, bb02, bb03, bb04, bb05;
    var arr;

    arr = test();
    aa01 = new Date();
    arr = arr.unique1();
    bb01 = new Date();
    output('unique1: ' + (bb01.getTime() - aa01.getTime()));

    arr = test();
    aa02 = new Date();
    arr = arr.unique2();
    bb02 = new Date();
    output('unique2: ' + (bb02.getTime() - aa02.getTime()));

    arr = test();
    aa03 = new Date();
    arr = arr.unique3(); 
    bb03 = new Date();
    output('unique3: ' + (bb03.getTime() - aa03.getTime()));

    arr = test();
    aa04 = new Date();
    arr = arr.unique4();
    bb04 = new Date();
    output('unique4: ' + (bb04.getTime() - aa04.getTime()));

    arr = test();
    aa05 = new Date();
    arr = arr.unique5(); // 这种最快
    bb05 = new Date();
    output('unique5: ' + (bb05.getTime() - aa05.getTime()));

  }
  </script>
</body>
</html>