3 methods and code instances for JavaScript arrays to go heavy

Source: Internet
Author: User
Tags arrays hash javascript array

This article mainly introduced the JavaScript array to go heavy 3 kinds of methods and the code example, this article directly gives the example code, needs the friend to be possible to refer to under

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.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67-68 /* 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=th Is.length; i < Len; i++) {if (Newarr.indexof (this[i)) = = 1) {//If I of the current array has been saved in a temporary array, then skip, otherwise 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}," Mystic figure "," Match man skill Fight "," Supersonic Battlefield "," Little Xin Dozen Bricks "," Match man skill Fight "," Garfield Superman "," Little Spice Bricks "," Despicable Me 2 "," Current Wire "," Flying Trolley "," God D secret Characters "," match man s skill Combat "," super Sound SD Speed Battlefield "," small SD Play Bricks "," match people SD skills combat "," Garfield S Cat Superman ", "Small df of the bricks", "Despicable FS I 2", "Electric D-Flow wire", "Flying SD trolley," Mysterious SD characters "," match man technology D skillful Combat "," super sound ASD fast battlefield "," little Xin Dozen Sad Bricks "," Match man technology SD skillful combat "," Garfield FDs cat Superman "," Little symplectic play 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 "," Xiao Xin Yi dozen Bricks, "despicable Yi I 2", "Current YT Wire", "Flying hand ytui cart", "God Dyu secret Characters", "The fire Yui Chai People s technology Yui skillfully Fighting", "Super Sound Sdyu Speed Battlefield", " Small SD Dozen Bricks Uyi block "," match Yui people sd skill latticeDou "," plus 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", " Small Xin Dozen SAHKD bricks "," match people technology SD Qiao GHK fighting "," 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 Dat E (). GetTime (); Console.log (t1); Start time   Arr.unique (); Go 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.