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 43 44 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 1: 1, '1' will be considered to be the same; all hash objects, such as: {x; 1}, {y: 1} will be considered the same // 10 ms */ 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 1 ultimate version: All hash objects, such as: {x; 1}, {y: 1}, will be considered the same // 30 ms */ 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 2: deduplication results are the best, but performance consumption // 250 ms */ 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 3: The hash object cannot be deduplicated. // 25 ms */ 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 I of the current array has been saved in a temporary array, skip this step. 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: "12", holobby: "football" },{ name: "pobaby1", age: "121", holobby: "football1" },{ x: 134 },{ y: 132 },{ x: 143 },{ y: 3421}, "mysterious character", "matcher skill combat", "Supersonic Battlefield", "Xiao Xin hitting bricks ", "matcher skill combat", "Garfield cat Superman", "Xiao Xin strike bricks", "mean me 2", "Current Wire", "Apsara trolley ", "Shen D secret character", "matchman S skill combat", "supersound SD speed Battlefield", "Small SD hitting bricks", "matcher SD skill fighting ", "garfi S cat Superman", "Little DF Xin strike bricks", "mean fs I 2", "Electric D flow conductor", "Apsara SD trolley ", "mysterious SD character", "matchgirl skill D skillful combat", "super sound ASD speedy Battlefield", "Xiao Xin beat SAD bricks", "matchgirl skill SD skillful combat ", "Garfield FDS cat Superman", "Xiao Xin hits the SDF brick", "mean SDF me 2", "Current SDF wire", "Apsara hand DF stroller ", "mysterious SD characters", "matchwood talents AS skillful combat", "Supersonic warfare FS Field", "xiaoxin SDF hitting bricks", "matchwood SDF fighting skills ", "Garfield SD cat Superman", 113,231,222 1334, 132, "", "21", {x: 13, y:}, {name: "pobaby2", age: "122", holobby: "football2" },{ name: "pobaby13", age: "1231", holobby: "football41" },{ x: 13544 },{ y: 1352 },{ x: 14543 },{ y: 34521}, "mysterious person sd things", "matcher skill sd skillful combat", "Supersonic sd Battlefield ", "Xiao Xin sd hitting bricks", "matcher skill gw fighting", "Garfield cat ui Superman", "Xiao Xin yi hitting bricks", "mean yi I 2 ", "Current yt wire", "Apsara mobile ytui stroller", "secret character of Shen Dyu", "Fire yui Chai Ren S technology yui clever combat", "supervoice SDyu speed Battlefield ", "Little SD Xin brick uyi block", "Match yui human SD skills combat", "add yui Fei S cat Superman", "Little DF Xin brick ui block ", "Mean uyi FS I 2", "e-D streaming yui line", "Apsara SD manual uyi car", "God I secret SD character ", "matchwood human skill Dhk clever combat", "super sound ASD fast combat hk", "Xiao Xin beat SAhkD bricks", "matchwood human skill SD clever ghk combat ", "Garfield FDS Cat k Superman", "Little Xin beat SDF brick ytui block", "mean SDF yui I 2", "Current SDyuF wire", "Apsara hand yuiDF stroller ", "Shen iy mi SD person hk things", "Match uyi person skill AS skill hk fighting", "uyin hg fast war FS Field", "Xiao Xin SDF bricks hjk block ", "fireworks SDF skill hj skillful combat", "Garfield SDhk cat Superman"]; /* 100,000 random data records */ 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 (); // deduplication Var t2 = new Date (). getTime (); console. log (t2); // End Time Console. log (t2-t1 ); |