JavaScript array 3 ways to go heavy and code instances _javascript tips

Source: Internet
Author: User
Tags javascript array

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

What kind of method to use, according to the actual situation and decided.

/* Method One: 1, ' 1 ' will be considered the same;
  All hash objects, such as: {x;1},{y:1} will be considered to be 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;
  }/* Method An improved version: All hash objects, such as: {x;1},{y:1} will be considered to be 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;
  }/* Method Two: The result is the best, but the 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++) {//Note = = if (
        Newarr[i]===newarr[j]) {newarr.splice (j,1);
      j--;
}} return NEWARR; /* Method Three: Cannot go heavy hash object//25ms/Array.prototype.unique = function () {var newArr = [];//A new temporary array for (VAR i= 0,len=this.length; i < Len; i++) {if (Newarr.indexof (this[i]) = = 1) {//If the current array has been saved in the temporary array, skip, or push the current item 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: "A", Hobby: "Football"},{name: "Pobaby1", Age: " 121 ", Hobby:" Football1 "},{x:134},{y:132},{x:143},{y:3421}," Mysterious character "," Match man skill Combat "," Supersonic Battlefield "," small xin Dozen Bricks "," match person skill Combat "," Garfield Superman "," Little play bricks, "mean Me 2", "Current Wire", "Flying cart", "God D secret Characters", "match man s skill fighting", "Super Sound SD Speed Battlefield", "small SD Play Bricks", "match people SD skill combat", "Garfield S Cat Superman", "small df xin Dozen Bricks", " Despicable FS I 2 "," "Electric D-Stream wire", "Flying SD Trolley", "Mysterious SD characters", "match man technology D skillful Combat", "super sound ASD fast battlefield", "little Xin Dozen Sad Bricks", "match people technology SD skillful combat", "Garfield FDs cat Superman", "small Xin dozen SDF Bricks", " Despicable SDF I 2 "," Current SDF Wire "," Flying hand df Trolley "," Mysterious SD characters "," match man technology as skillful combat "," Supersonic Battle fs Field "," small xin SDF Dozen bricks "," match man SDF skill Combat "," Garfield SD Cat Superman ", 113,231,2221,123,234, "1334", "a", {x:13,y:132},{name: "Pobaby2", Age: "122", Hobby: "Football2"},{name: "Pobaby13", Age: "1231", Hobby: "Football41"},{x:13544},{y:1352},{x:14543},{y:34521}, "Mysterious person SD", "Match man technology SD skillful combat", "Supersonic SD Battlefield", "small xin SD Brick "," Match man skills GW Combat "," Garfield UI Superman "," Little Sin Yi DozenBricks "," Vile Yi I 2 "," Current YT wire, "flying hand ytui cart", "God Dyu secret Characters", "The fire Yui Chai Person s technology Yui skillful fighting", "Super Sound Sdyu Speed Battlefield", "small SD dozen Bricks Uyi block", "match Yui people SD skills Combat", " Add Yui Philippine Cat Superman "," small df Xin Dozen brick UI block "," the despicable Uyi FS I 2 "," the electric D flow leads the Yui line, "The Flying SD hand pushes the Uyi car", "The god I secret SD person", "the match person technique dhk skillfully fights", "the Ultra sound ASD Super HK field", "The small Xin dozen SAHKD Bricks" , "Match man technique SD Qiao GHK combat", "Garfield FDs cat K Superman," small Xin dozen SDF brick Ytui block "," despicable SDF Yui I 2 "," Current Sdyuf Wire "," Flying hand yuidf cart "," God iy secret SD people HK ","

Match Uyi person technology as Qiao "," "super-Tone HG Super fs field", "small xin SDF dozen Bricks HJK block", "Match man SDF technology HJ skillful combat", "Garfield Sdhk cat Superman"];
/* 100,000 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); Start time Arr.unique (); Go to heavy var t2 = new Date (). GetTime (); Console.log (T2);
 End Time Console.log (T2-T1);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.