欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript數(shù)字?jǐn)?shù)組去重復(fù)項(xiàng)的實(shí)現(xiàn)代碼

 更新時(shí)間:2010年12月30日 19:33:57   作者:  
console.log 不支持ie,下面的代碼需要在火狐中測(cè)試,不然會(huì)有問(wèn)題。
test.htm
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>

</head>

<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; x < 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();//數(shù)組很大時(shí),最快

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>
</html>

Watch.js
復(fù)制代碼 代碼如下:

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(200 * 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];
}
}

相關(guān)文章

  • JS獲取填報(bào)擴(kuò)展單元格控件的值的解決辦法

    JS獲取填報(bào)擴(kuò)展單元格控件的值的解決辦法

    這篇文章主要介紹了JS獲取填報(bào)擴(kuò)展單元格控件的值的解決辦法,需要的朋友可以參考下
    2017-07-07
  • javascript中閉包(Closure)詳解

    javascript中閉包(Closure)詳解

    閉包(closure)是Javascript語(yǔ)言的一個(gè)難點(diǎn),也是它的特色,很多高級(jí)應(yīng)用都要依靠閉包實(shí)現(xiàn)。小編之前一直糊里糊涂的,沒(méi)有能夠弄明白JavaScript的閉包到底是什么,有什么用,本文把自己的理解些出來(lái)分享一下,希望不理解JavaScript閉包的朋友們看了之后能夠理解閉包!
    2016-01-01
  • firefox中用javascript實(shí)現(xiàn)鼠標(biāo)位置的定位

    firefox中用javascript實(shí)現(xiàn)鼠標(biāo)位置的定位

    firefox中用javascript實(shí)現(xiàn)鼠標(biāo)位置的定位...
    2007-06-06
  • javascript遇到html5的一些表單屬性

    javascript遇到html5的一些表單屬性

    這篇文章主要介紹了javascript遇到html5的一些表單屬性的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • js 獲取元素在頁(yè)面上的偏移量的方法匯總

    js 獲取元素在頁(yè)面上的偏移量的方法匯總

    javascript可以通過(guò)四個(gè)屬性可以獲得元素的偏移量,分別是1、offsetHeight,2、offsetWidth,3、offsetLeft,4、offsetTop,今天我們就來(lái)具體談一下獲取頁(yè)面元素偏移量的最佳方法。
    2015-04-04
  • JavaScript實(shí)現(xiàn)即時(shí)通訊的 4 種方案

    JavaScript實(shí)現(xiàn)即時(shí)通訊的 4 種方案

    這篇文章主要給大家分享了JavaScript實(shí)現(xiàn)即時(shí)通訊的 4 種方案,其實(shí)就是服務(wù)端如何將數(shù)據(jù)推送到瀏覽器,下面詳細(xì)的文章內(nèi)容,需要的小伙伴參考一下,洗碗給對(duì)你有所幫助
    2022-02-02
  • 腳本合并提升javascript性能示例

    腳本合并提升javascript性能示例

    腳本合并可以有效提升javascript性能,下面有個(gè)不錯(cuò)的示例,大家可以參考下
    2014-02-02
  • JavaScript Spread Syntax (...)的十種使用方法

    JavaScript Spread Syntax (...)的十種使用方法

    這篇文章主要介紹了JavaScript Spread Syntax (...)的十個(gè)強(qiáng)大用途,擴(kuò)展語(yǔ)法即Spread Syntax(…) 是 ES6 中引入的一個(gè)新特性,它允許我們從可迭代對(duì)象中快速提取元素
    2022-07-07
  • javascript運(yùn)行機(jī)制之執(zhí)行順序理解

    javascript運(yùn)行機(jī)制之執(zhí)行順序理解

    這篇文章主要介紹了javascript運(yùn)行機(jī)制之執(zhí)行順序理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 基于layui內(nèi)置模塊(element常用元素的操作)

    基于layui內(nèi)置模塊(element常用元素的操作)

    今天小編就為大家分享一篇基于layui內(nèi)置模塊(element常用元素的操作),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09

最新評(píng)論