nam: Herausfinden ob ein Wert im Array vorhanden ist

Beitrag lesen

Hallo Struppi

Du forderst mich heraus! ;-)

Auch ich hab ein kleines Benchmark gemacht (brauchte halt ein bisschen länger als du, aber egal).
Ich ging ein bisschen anders an die Sache ran (vielleicht ein bisschen mehr 'real live') und bei mir gewinnt ganz klar deine Variante mit dem Object:

  
Array.prototype.hasValue=function(val) {  
    for(var i=0; i<this.length; i++) {  
     if( val == this[i]) {  
      return true;  
     }  
    }  
    return false;  
};  
  
Array.prototype.toObject=function() {  
 var tmp={};  
    for(var i=0; i<this.length; i++) {  
  tmp[this[i]]=true;  
 }  
 return tmp;  
}  
  
Array.prototype.cleanUp=function() {  
 for(var i=0; i<this.length; i++) {  
  if(this[i]==this[i+1]) {  
   this.splice(i,1);  
  }  
 }  
}  
  
//Vorhandenes Array  
var a=new Array('a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1', 'j1', 'k1', 'l1', 'm1', 'n1', 'o1', 'p1', 'q1', 'r1', 's1', 't1', 'u1', 'v1', 'w1', 'x1', 'y1', 'z1', 'a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 'i2', 'j2', 'k2', 'l2', 'm2', 'n2', 'o2', 'p2', 'q2', 'r2', 's2', 't2', 'u2', 'v2', 'w2', 'x2', 'y2', 'z2', 'a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3', 'j3', 'k3', 'l3', 'm3', 'n3', 'o3', 'p3', 'q3', 'r3', 's3', 't3', 'u3', 'v3', 'w3', 'x3', 'y3', 'z3', 'a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4', 'j4', 'k4', 'l4', 'm4', 'n4', 'o4', 'p4', 'q4', 'r4', 's4', 't4', 'u4', 'v4', 'w4', 'x4', 'y4', 'z4', 'a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5', 'i5', 'j5', 'k5', 'l5', 'm5', 'n5', 'o5', 'p5', 'q5', 'r5', 's5', 't5', 'u5', 'v5', 'w5', 'x5', 'y5', 'z5', 'a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6', 'i6', 'j6', 'k6', 'l6', 'm6', 'n6', 'o6', 'p6', 'q6', 'r6', 's6', 't6', 'u6', 'v6', 'w6', 'x6', 'y6', 'z6', 'a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7', 'j7', 'k7', 'l7', 'm7', 'n7', 'o7', 'p7', 'q7', 'r7', 's7', 't7', 'u7', 'v7', 'w7', 'x7', 'y7', 'z7', 'a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8', 'i8', 'j8', 'k8', 'l8', 'm8', 'n8', 'o8', 'p8', 'q8', 'r8', 's8', 't8', 'u8', 'v8', 'w8', 'x8', 'y8', 'z8', 'a9', 'b9', 'c9', 'd9', 'e9', 'f9', 'g9', 'h9', 'i9', 'j9', 'k9', 'l9', 'm9', 'n9', 'o9', 'p9', 'q9', 'r9', 's9', 't9', 'u9', 'v9', 'w9', 'x9', 'y9', 'z9', 'a10', 'b10', 'c10', 'd10', 'e10', 'f10', 'g10', 'h10', 'i10', 'j10', 'k10', 'l10', 'm10', 'n10', 'o10', 'p10', 'q10', 'r10', 's10', 't10', 'u10', 'v10', 'w10', 'x10', 'y10', 'z10', 'a20', 'b20', 'c20', 'd20', 'e20', 'f20', 'g20', 'h20', 'i20', 'j20', 'k20', 'l20', 'm20', 'n20', 'o20', 'p20', 'q20', 'r20', 's20', 't20', 'u20', 'v20', 'w20', 'x20', 'y20', 'z20', 'a30', 'b30', 'c30', 'd30', 'e30', 'f30', 'g30', 'h30', 'i30', 'j30', 'k30', 'l30', 'm30', 'n30', 'o30', 'p30', 'q30', 'r30', 's30', 't30', 'u30', 'v30', 'w30', 'x30', 'y30', 'z30', 'a40', 'b40', 'c40', 'd40', 'e40', 'f40', 'g40', 'h40', 'i40', 'j40', 'k40', 'l40', 'm40', 'n40', 'o40', 'p40', 'q40', 'r40', 's40', 't40', 'u40', 'v40', 'w40', 'x40', 'y40', 'z40', 'a50', 'b50', 'c50', 'd50', 'e50', 'f50', 'g50', 'h50', 'i50', 'j50', 'k50', 'l50', 'm50', 'n50', 'o50', 'p50', 'q50', 'r50', 's50', 't50', 'u50', 'v50', 'w50', 'x50', 'y50', 'z50', 'a60', 'b60', 'c60', 'd60', 'e60', 'f60', 'g60', 'h60', 'i60', 'j60', 'k60', 'l60', 'm60', 'n60', 'o60', 'p60', 'q60', 'r60', 's60', 't60', 'u60', 'v60', 'w60', 'x60', 'y60', 'z60', 'a70', 'b70', 'c70', 'd70', 'e70', 'f70', 'g70', 'h70', 'i70', 'j70', 'k70', 'l70', 'm70', 'n70', 'o70', 'p70', 'q70', 'r70', 's70', 't70', 'u70', 'v70', 'w70', 'x70', 'y70', 'z70', 'a80', 'b80', 'c80', 'd80', 'e80', 'f80', 'g80', 'h80', 'i80', 'j80', 'k80', 'l80', 'm80', 'n80', 'o80', 'p80', 'q80', 'r80', 's80', 't80', 'u80', 'v80', 'w80', 'x80', 'y80', 'z80', 'a90', 'b90', 'c90', 'd90', 'e90', 'f90', 'g90', 'h90', 'i90', 'j90', 'k90', 'l90', 'm90', 'n90', 'o90', 'p90', 'q90', 'r90', 's90', 't90', 'u90', 'v90', 'w90', 'x90', 'y90', 'z90');  
  
//Diese Werte sollen in a eingefügt werden  
var n=new Array('a0', 'b0', 'c0', 'd0', 'e0', 'f0', 'g0', 'h0', 'i0', 'j0', 'k0', 'l0', 'm0', 'n0', 'o0', 'p0', 'q0', 'r0', 's0', 't0', 'u0', 'v0', 'w0', 'x0', 'y0', 'z0', 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1', 'j1', 'k1', 'l1', 'm1', 'n1', 'o1', 'p1', 'q1', 'r1', 's1', 't1', 'u1', 'v1', 'w1', 'x1', 'y1', 'z1', 'z90');  
  
function benchmark() {  
 //mit hasValue:  
 d1=new Date();  
 for(var i=0; i<n.length; i++) {  
  if(!a.hasValue(n[i])) {  
   a.push(n[i]);  
  }  
 }  
 d2=new Date();  
 alert("hasValue:"+(d2-d1));  
  
 //mit Obj  
 d1=new Date();  
 var o=a.toObject();  
 for(var i=0; i<n.length; i++) {  
  if(!o[n[i]]) {  
   o[n[i]]=true;  
  }  
 }  
 d2=new Date();  
 alert("toObject:"+(d2-d1));  
  
 //mit cleanUp  
 d1=new Date();  
 for(var i=0; i<n.length; i++) {  
  a.push(n[i]);  
 }  
 a.sort();  
 a.cleanUp();  
 d2=new Date();  
 alert("cleanUp:"+(d2-d1));  
}  

Meine Benchmarks (auf einem älteren PowerMac 933MHz, bzw. VirtualPC) haben ergeben:

IE6/7: Variante mit Object gewinnt, Variante hasValue und cleanUp sind beide gleich langsam.
Firefox und Safari: Variante mit Object gewinnt, cleanUp ist aber nur wenig langsamer. hasValue ist viel langsamer.

Kurz:
hasValue in den Papierkorb und mit Object arbeiten!

Für ein schöneres Web!

Gruss aus Zürich,
Mathias