Test.htm
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>array-remove-repeate</title>
<style>
. tt{Background-color: #006699; height:3px; overflow:hidden;}
</style>
<body>
<div class= "tt" ></div>
<div class= "Result" id= "result" ></div>
<script>
if (!console)
{
var console={};
Console.log=function (str) {alert (str);}
}
Array.prototype.unique1 = function () {
var r = new Array ();
Label:for (var i = 0, n = this.length; i < n; i++) {
for (var x = 0, y = r.length < y; x + +) {
if (r[x] = = This[i]) {
Continue label;
}
}
R[r.length] = this[i];
}
return R;
}
Array.prototype.unique2 = function () {
Return This.sort (). Join (",,"). Replace (/(, |^) ([^,]+) (,, \2) + (, |$)/g, "$1$2$4"). Replace (/,,+/g, ","). Replace (/,$/, "" " ). Split (",");
}
Array.prototype.unique3 = function () {
var temp = {}, len = This.length;
for (Var i=0 i < Len; i++) {
var tmp = this[i];
if (!temp.hasownproperty (TMP)) {
Temp[this[i]] = "My God";
}
}
len = 0;
var temparr=[];
for (var i in temp) {
temparr[len++] = i;
}
return Temparr;
}
Array.prototype.unique4 = function () {
var temp = new Array ();
This.sort ();
for (i = 0; i < this.length; i++) {
if (this[i] = = This[i+1]) {
Continue
}
Temp[temp.length]=this[i];
}
return temp;
}
var test= (function ()
{
var arr2=[];
for (Var i=0;i<2000;i++)
{
var t=i;
T=parseint (Math.random () *2000) +1;
Arr2[i]= (T.tostring ());
}
arr2=["Zhoujian", "Zhou", "Zhou"];
return function () {
return arr2;
return [1,2,3,3];
};
})();
Window.onload=function () {
//
Watch.start ("Cost times1:");
var arr= test ();
Console.log (arr.length);
Arr=arr.unique1 ();
Console.log (arr.length);
Watch.stop ();
//
Watch.start ("Cost Times2:");
arr= test ();
Console.log (arr.length);
Arr=arr.unique2 ();
Console.log (arr.length);
Watch.stop ();
//
Watch.start ("Cost Times3:");
arr= test ();
Console.log (arr.length);
Arr=arr.unique3 ();//array is large when the fastest
Console.log (arr.length);
Watch.stop ();
//
Watch.start ("Cost times4:");
arr= test ();
Console.log (arr.length);
Arr=arr.unique4 ();
Console.log (arr.length);
Watch.stop ();
Watch.report ();
}
</script>
</body>
Watch.js
Copy Code code as follows:
var Watch = {
Result: [],
GUID:-1,
totaltime:0,
Start:function (title) {
This.result[++this.guid] = [title | | this.guid, New Date (). GetTime ()];
},
Stop:function () {
var r = This.result[this.guid];
var t = new Date (). GetTime ()-r[1];
This.totaltime + = t;
R[1] = t;
if (t>=10000) {
Alert ("This code takes too long to run,you should optimizate them.");
}
},
Report:function (parent) {
var div = document.createelement ("div");
Div.style.fontSize = "12px";
var str = [];
Str.push ("<p><b>the total Times:</b><span style= ' color: #f00 ' >" + this.totaltime + "</span > ms.</p> ");
for (var i = 0, L = this.result.length i < l; i++) {
if (This.result[i].length > 1) {
Str.push ("<p>" + "<span style= ' Width:200px;display:inline-block;background-color: #f7f7f7;- moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px; ' > "+" <span style= ' Background-color: #0c0; -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;width= "+ (This.totaltime = = 0? this.totalTime: parseint (* this.result[i][1]/this.totaltime)) + "PX; Display:inline-block; ' > "+this.result[i][1]+" </span> "+" </span> <span style= ' width:150px; Display:inline-block; ' > "+ this.result[i][0] +" </span> "+" </p> ");
}else{
Str.push (This.result[i][0]);
}
}
div.innerhtml = Str.join ("");
Parent = Parent | | Document.body;
Parent.appendchild (DIV);
div = null;
This.totaltime = 0;
This.guid =-1;
This.result=[];
},
Fns:function () {
var a = arguments;
for (var i = 0, L = a.length i < l; i++) {
This.start (A[i][0]);
A[I][1] ();
This.stop ();
}
},
Execbytimes:function (FN, times, title) {
This.start (title);
while (times--) {
FN ();
}
This.stop ();
},
Print:function (str) {
THIS.RESULT[++THIS.GUID]=[STR];
}
}