首页 > 编程 > JavaScript > 正文

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

2019-11-19 17:35:29
字体:
来源:转载
供稿:网友

其上搜索些方法一堆堆的,之所以一遍主要是因自己一之後就忘了其他方法怎麽,就一一下,便做看看哪效率最高,了更好展示效果,我先比好的方法,後面一。(馨提示:下文是了便,一般情下不建在原型上,容易污染全局)

一,通找象性

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

方法算: 先排序,再比前後字是不是相等 > 通找象性 > 通找位置 > 搜索字第一次出的位置是不是跟前位置一

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表