美文网首页
数组去重方法速度对比

数组去重方法速度对比

作者: fighterboy | 来源:发表于2017-10-18 11:09 被阅读0次

面试时经常被问到数组去重的问题,个人觉得这种问题其实没太大的作用,毕竟在实际工作中很少能遇到的很大的数组,所以不论以什么样的方式实现都很难做到内存的溢出及产生较大的耗时差距(除非你是故意)               今天咱们对比一下那个速度更快

function createTestArray(){

const testArray=[1,2,3,4,5,'a','b','c','d','e'];

for(let i=0;i<13;i++){

testArray.push(...testArray);

}

testArray.sort((a,b)=>Math.random()>0.5?1:-1);

//console.log(`testArray:${testArray},length:${testArray.length}`);

return testArray;

}

const testArray=createTestArray();

function showTime(fun){

const start=new Date();

const result=fun();

return `name:${fun.name},time:${new Date()-start},result:${result.join(',')}`;

}

function removeRepeatBySet(){

return [...new Set(testArray)];

}

function removeRepeatByFilter(){

return testArray.filter((item,index,arr)=>arr.indexOf(item)==index);

}

function removeRepeatByForof(){

const obj={},arr=[],value={};

for(let i of testArray){

if(obj[i]==value){

continue;

}else if(obj[i]!=value){

obj[i]=value;

arr.push(i);

}

}

return arr;

}

function removeRepeatByFor(){

const obj={},arr=[],value={};

for(let i= 0,len=testArray.length;i

const item=testArray[i];

if(obj[item]==value){

continue;

}else if(obj[item]!=value){

obj[item]=value;

arr.push(item);

}

}

return arr;

}

const results=[];

for(var i=0;i<10;i++){

results.push(

showTime(removeRepeatBySet),

showTime(removeRepeatByForof),

showTime(removeRepeatByFilter),

showTime(removeRepeatByFor)

);

}

console.log(JSON.stringify(results.sort(),null,4));

打印的结果

[

"name:removeRepeatByFilter,time:10,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:10,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:12,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:6,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:7,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:7,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:8,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:2,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:5,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByFor,time:6,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:11,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:6,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:6,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:7,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:10,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:13,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:16,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:6,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:7,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:7,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:8,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d",

"name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d"

]

此时我们可以看出。  for循环是用时最少的,其次for of,filter,而es6的Set用时最长

相关文章

  • 数组去重方法速度对比

    面试时经常被问到数组去重的问题,个人觉得这种问题其实没太大的作用,毕竟在实际工作中很少能遇到的很大的数组,所以不论...

  • js 数组去重方法对比

    1 最简单方法 2 使用 Map 保存 3 数组排序,只添加前面不同的值 4 数组下标法 5 将最后出现的值加入...

  • ES6数组去重

    普通数组去重 方法1 方法2 对象数组去重

  • js数组去重、对象数组去重

    普通数组去重 一、普通数组去重 方法一:遍历数组法 方法二:排序法 方法三:对象法 对象数组去重 方法一:将对象数...

  • Array集结号

    实现数组去重的几种方法 数组去重一 数组去重二 利用数组indexof+push实现数组去重 数组去重三 利用对象...

  • 数组

    数组 数组常用方法 数组去重

  • 数组去重

    分类 非对象数组去重 对象数组去重 分类一 --- 非对象数组去重 方法一: set(es6常用) 方法二:red...

  • 2月份第二次总结(数组去重)

    数组去重的方法 1.定义新数组,将元素和新数组进行对比(设一个布尔值 或者 用indexOf) 2.将数组排序,比...

  • 数组对象去重方法:

    数组对象去重方法: // 数组对象去重 ```` toRetry = (arr = []) => { let re...

  • js:数组去重

    数组去重的常见写法: 数组去重封装成方法: es6的数组去重(Array.from):

网友评论

      本文标题:数组去重方法速度对比

      本文链接:https://www.haomeiwen.com/subject/kcmsuxtx.html