Three methods and code instances for JavaScript array de duplication

  • 2020-06-22 23:55:52
  • OfStack

There are many ways to remove the weight of the array, which is the most ideal, I do not know. So I tested the effect and performance of the array deweighting. Test 100,000 data, code, and approximate time as follows.

Which method to adopt depends on the actual situation.


/* methods 1 :  1,'1'  It would be considered the same ;  all hash object , Such as: {x;1} . {y:1} It would be considered the same  //10ms */
Array.prototype.unique=function(){
  var newArr=[],obj={};
  for(var i=0,len=this.length;i<len;i++){
    if(!obj[this[i]]){ 
      newArr.push(this[i]);
      obj[this[i]]=true;
    }
  }
  return newArr;
}

/* methods 1 Improved version: All hash object , Such as: {x;1} . {y:1} It would be considered the same  //30ms*/
Array.prototype.unique=function(){
  var newArr=[],obj={};
  for(var i=0,len=this.length;i<len;i++){
    if(!obj[typeof(this[i])+this[i]]){ 
      newArr.push(this[i]);
      obj[typeof(this[i])+this[i]]=this[i];
    }
  }
  return newArr;
}

/* methods 2 :   De-weighting yields the best results, but costs performance    //250ms*/
Array.prototype.unique=function(){
  var newArr=this.concat();
  for(var i=0,len=newArr.length;i<len;i++) {
    for(var j=i+1,len=newArr.length;j<len;j++) {
      // Pay attention to  ===
      if(newArr[i]===newArr[j]) {
        newArr.splice(j,1);
        j--;
      }
    }
  }
  return newArr;
}

/* methods 3 :   Can't go to heavy hash object  //25ms */
Array.prototype.unique = function(){
  var newArr = []; //1 A new temporary array 
  for(var i = 0,len=this.length; i < len; i++){    
    if (newArr.indexOf(this[i]) == -1){  // If the first of the current array i It's already in a temporary array, so skip it , Otherwise put the current item push Go to the temporary array 
      newArr.push(this[i]);
    }
  }
  return newArr;
}


var arr0=[11,21,221,13,24,"134","1",{x:1,y:1},{name:"pobaby",age:"12",hobby:"football"},{name:"pobaby1",age:"121",hobby:"football1"},{x:134},{y:132},{x:143},{y:3421}," The mysterious figure ", " Matchmaker skill fighting ", " Supersonic battlefield ", " Little Xin breaks bricks ", " Matchmaker skill fighting ", " Garfield Superman ", " Little Xin breaks bricks ", " Despicable me 2", " Current lead ", " Flying trolley "," god D The secret character ", " Match people S Techniques to combat ", " Super sound SD A quick field ", " A small SD Sheen playing bricks ", " Match people SD Techniques to combat ", " Garfield S The cat superman ", " A small DF Sheen playing bricks ", " despicable FS I 2", " electricity D Flow conductor ", " flying SD A cart "," mysterious SD figure ", " Match people skills D Qiao combat ", " Super sound ASD A quick field ", " A little sheen to play SAD The brick ", " Match people skills SD Qiao combat ", " Garfield FDS The cat superman ", " A little sheen to play SDF The brick ", " mean SDF The I 2", " current SDF conductor ", " Flying hands DF cart "," mysterious SD figure ", " Match people skills AS Qiao combat ", " The supersonic war FS field ", " A little sheen SDF Make bricks ", " Match people SDF Techniques to combat ", " Garfield SD The cat superman ",113,231,2221,123,234,"1334","21",{x:13,y:132},{name:"pobaby2",age:"122",hobby:"football2"},{name:"pobaby13",age:"1231",hobby:"football41"},{x:13544},{y:1352},{x:14543},{y:34521}," Mystery man sd things ", " Match people skills sd Qiao combat ", " supersonic sd The battlefield ", " A little sheen sd Make bricks ", " Matchmaker skills gw combat ", " Garfield ui superman ", " A little sheen yi Make bricks ", " despicable yi I 2", " current yt conductor ", " Flying hands ytui cart "," god Dyu The secret character ", " The fire yui Wood people S The skill yui Qiao combat ", " Super sound SDyu A quick field ", " A small SD Sheen playing brick uyi block ", " A match yui people SD Techniques to combat ", " add yui fe S The cat superman ", " A small DF Sheen playing brick ui block ", " mean uyi the FS I 2", " electricity D The flow guide yui line ", " flying SD push uyi car "," god i The secret SD figure ", " Match people skills Dhk Qiao combat ", " Super sound ASD A quick hk field ", " A little sheen to play SAhkD The brick ", " Match people skills SD qiao ghk combat ", " Garfield FDS The cat k superman ", " A little sheen to play SDF brick ytui block ", " mean SDF the yui I 2", " current SDyuF conductor ", " Flying hands yuiDF cart "," god iy The secret SD people hk things ", " A match uyi People skills AS Smart grid hk bucket ", " Super sound hg A quick FS field ", " A little sheen SDF A brick hjk block ", " Match people SDF The skill hj Qiao combat ", " Garfield SDhk The cat superman " ];

/*10 Ten thousand random data */
var arr=[],num;
for(var i = 0; i < 100000; i++){
  num=Math.floor(Math.random()*50);
  arr.push(arr0[num]);
}


var t1= new Date().getTime(); console.log(t1); // The start time 

arr.unique(); // duplicate removal 

var t2 = new Date().getTime(); console.log(t2); // The end of time 

console.log(t2-t1);


Related articles: