詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間_第1頁(yè)
詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間_第2頁(yè)
詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間_第3頁(yè)
詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間_第4頁(yè)
詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、詳解JS統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間、統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)常規(guī)的方法可以使用輸出來(lái)肉眼計(jì)算有多少個(gè)輸出不過(guò)在中內(nèi)置了一個(gè)方法,可以統(tǒng)計(jì)一個(gè)字符串輸出的次數(shù)。我們可以利用這個(gè)來(lái)間接地統(tǒng)計(jì)函數(shù)的執(zhí)行次數(shù)已經(jīng)執(zhí)行已經(jīng)執(zhí)行已經(jīng)執(zhí)行已經(jīng)執(zhí)行已經(jīng)執(zhí)行不帶參數(shù)則為值,否則將會(huì)輸出該字符串的執(zhí)行次數(shù),觀測(cè)起來(lái)還是挺方便的當(dāng)然,除了輸出次數(shù)之外,還想獲取一個(gè)純粹的次數(shù)值,可以用裝飾器將函數(shù)包裝一下,內(nèi)部使用對(duì)象存儲(chǔ)調(diào)用次數(shù)即可裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù)在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù)執(zhí)/行/次數(shù)給添加裝飾器,執(zhí)行前將進(jìn)行計(jì)數(shù)累加存儲(chǔ)的值不重/復(fù)/綁定,有則返回綁定/計(jì)數(shù)累加/定義/函/數(shù)的值為計(jì)數(shù)

2、值(初始化)functionotherFunction()someFunction=getFunCallTimes(someFunction,someFunction);someFunction();/count1someFunction();/count2someFunction();/count3someFunction();/count4console.log(someFunction.callTimes);/4otherFunction=getFunCallTimes(otherFunction);otherFunction();/count1console.log(otherFunc

3、tion.callTimes);/1otherFunction();/count2console.log(otherFunction.callTimes);/2二、統(tǒng)計(jì)函數(shù)執(zhí)行時(shí)間Chrome中內(nèi)置了console.time和console.timeEnd來(lái)打點(diǎn)計(jì)算時(shí)間console.time();for(vari=0;i100000;+i)console.timeEnd();/default:1.77197265625ms不傳入?yún)?shù)的話,將以default輸出毫秒值我們可以封裝一下,傳入函數(shù)名稱,類似上面的做法,使用裝飾器在函數(shù)執(zhí)行前后進(jìn)行處理vargetFunExecTime=(funct

4、ion()/裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù)functiondecoratorBefore(fn,beforeFn)returnfunction()varret=beforeFn.apply(this,arguments);/在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù)if(ret!=false)fn.apply(this,arguments);/裝飾器,在當(dāng)前函數(shù)執(zhí)行后執(zhí)行另一個(gè)函數(shù)functiondecoratorAfter(fn,afterFn)returnfunction()fn.apply(this,arguments);afterFn.apply(this,arguments);

5、/執(zhí)行次數(shù)varfunTimes=;/給fun添加裝飾器,fun執(zhí)行前后計(jì)時(shí)returnfunction(fun,funName)returndecoratorAfter(decoratorBefore(fun,function()/執(zhí)行前console.time(funName);),function()/執(zhí)行后console.timeEnd(funName););)();那么調(diào)用的時(shí)候,就不需要理會(huì)如何計(jì)時(shí)了functionsomeFunction()for(vari=0;i100000;+i)functionotherFunction()for(vari=0;i10000000;+i)s

6、omeFunction=getFunExecTime(someFunction,someFunction);someFunction();/someFunction:1.616943359375msotherFunction=getFunExecTime(otherFunction,otherFunction);otherFunction();/otherFunction:18.157958984375msChrome的ConsoleAP畢竟不是標(biāo)準(zhǔn)的,除了使用它之外,還可以選擇日期插件Date中的getTimenow相關(guān)方法然而使用Date對(duì)象來(lái)計(jì)算耗時(shí)并不正統(tǒng),推薦使用標(biāo)準(zhǔn)的perform

7、ance.nowvarstart=performance.now();console.time();for(vari=0;i10000000;+i)varend=performance.now();console.timeEnd();/default:23.598876953125msconsole.log(end-start);/23.600000015459955可以看到,它們是相差不大的使用類似的方法,將它包裝起來(lái)以便方便調(diào)用vargetFunExecTime=(function()/裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù)functiondecoratorBefore(fn,befor

8、eFn)returnfunction()varret=beforeFn.apply(this,arguments);/在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù)if(ret!=false)fn.apply(this,arguments);/裝飾器,在當(dāng)前函數(shù)執(zhí)行后執(zhí)行另一個(gè)函數(shù)functiondecoratorAfter(fn,afterFn)returnfunction()fn.apply(this,arguments);afterFn.apply(this,arguments);/執(zhí)行次數(shù)varfunTimes=;/給fun添加裝飾器,fun執(zhí)行前后計(jì)時(shí)returnfunction(fun,f

9、unName)funName=funName|fun;if(funTimesfunName)returnfunTimesfunName;/綁定funTimesfunName=decoratorAfter(decoratorBefore(fun,function()/執(zhí)行前funTimesfunName.timestampStart=performance.now();),function()/執(zhí)行后funTimesfunName.timestampEnd=performance.now();將執(zhí)行耗時(shí)存入funTimesfunName.valueOf=function()returnthis.

10、timestampEnd-this.timestampStart;);returnfunTimesfunName;)();functionsomeFunction()for(vari=0;i100000;+i)functionotherFunction()for(vari=0;i0)/執(zhí)行函數(shù)returnfun.apply(this,arguments);elseif(nextFun&typeofnextFun=function)/執(zhí)行下一個(gè)函數(shù)returnnextFun.apply(this,arguments);varfun=setFunCallMaxTimes(someFunction,3,otherFunction);fu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論