詳解JS中統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)與執(zhí)行時(shí)間
一、統(tǒng)計(jì)函數(shù)執(zhí)行次數(shù)
常規(guī)的方法可以使用 console.log 輸出來(lái)肉眼計(jì)算有多少個(gè)輸出
不過(guò)在Chrome中內(nèi)置了一個(gè) console.count 方法,可以統(tǒng)計(jì)一個(gè)字符串輸出的次數(shù)。我們可以利用這個(gè)來(lái)間接地統(tǒng)計(jì)函數(shù)的執(zhí)行次數(shù)
function someFunction() { console.count('some 已經(jīng)執(zhí)行'); } function otherFunction() { console.count('other 已經(jīng)執(zhí)行'); } someFunction(); // some 已經(jīng)執(zhí)行: 1 someFunction(); // some 已經(jīng)執(zhí)行: 2 otherFunction(); // other 已經(jīng)執(zhí)行: 1 console.count(); // default: 1 console.count(); // default: 2
不帶參數(shù)則為 default 值,否則將會(huì)輸出該字符串的執(zhí)行次數(shù),觀測(cè)起來(lái)還是挺方便的
當(dāng)然,除了輸出次數(shù)之外,還想獲取一個(gè)純粹的次數(shù)值,可以用裝飾器將函數(shù)包裝一下,內(nèi)部使用對(duì)象存儲(chǔ)調(diào)用次數(shù)即可
var getFunCallTimes = (function() { // 裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù) function decoratorBefore(fn, beforeFn) { return function() { var ret = beforeFn.apply(this, arguments); // 在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù) if (ret !== false) { fn.apply(this, arguments); } }; } // 執(zhí)行次數(shù) var funTimes = {}; // 給fun添加裝飾器,fun執(zhí)行前將進(jìn)行計(jì)數(shù)累加 return function(fun, funName) { // 存儲(chǔ)的key值 funName = funName || fun; // 不重復(fù)綁定,有則返回 if (funTimes[funName]) { return funTimes[funName]; } // 綁定 funTimes[funName] = decoratorBefore(fun, function() { // 計(jì)數(shù)累加 funTimes[funName].callTimes++; console.log('count', funTimes[funName].callTimes); }); // 定義函數(shù)的值為計(jì)數(shù)值(初始化) funTimes[funName].callTimes = 0; return funTimes[funName]; } })();
function someFunction() { } function otherFunction() { } someFunction = getFunCallTimes(someFunction, 'someFunction'); someFunction(); // count 1 someFunction(); // count 2 someFunction(); // count 3 someFunction(); // count 4 console.log(someFunction.callTimes); // 4 otherFunction = getFunCallTimes(otherFunction); otherFunction(); // count 1 console.log(otherFunction.callTimes); // 1 otherFunction(); // count 2 console.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 (var i = 0; i < 100000; ++i) { } console.timeEnd(); // default: 1.77197265625ms
不傳入?yún)?shù)的話,將以default輸出毫秒值
我們可以封裝一下,傳入函數(shù)名稱,類似上面的做法,使用裝飾器在函數(shù)執(zhí)行前后進(jìn)行處理
var getFunExecTime = (function() { // 裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù) function decoratorBefore(fn, beforeFn) { return function() { var ret = beforeFn.apply(this, arguments); // 在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù) if (ret !== false) { fn.apply(this, arguments); } }; } // 裝飾器,在當(dāng)前函數(shù)執(zhí)行后執(zhí)行另一個(gè)函數(shù) function decoratorAfter(fn, afterFn) { return function() { fn.apply(this, arguments); afterFn.apply(this, arguments); }; } // 執(zhí)行次數(shù) var funTimes = {}; // 給fun添加裝飾器,fun執(zhí)行前后計(jì)時(shí) return function(fun, funName) { return decoratorAfter(decoratorBefore(fun, function() { // 執(zhí)行前 console.time(funName); }), function() { // 執(zhí)行后 console.timeEnd(funName); }); } })();
那么調(diào)用的時(shí)候,就不需要理會(huì)如何計(jì)時(shí)了
function someFunction() { for (var i = 0; i < 100000; ++i) { } } function otherFunction() { for (var i = 0; i < 10000000; ++i) { } } someFunction = getFunExecTime(someFunction, 'someFunction'); someFunction(); // someFunction: 1.616943359375ms otherFunction = getFunExecTime(otherFunction, 'otherFunction'); otherFunction(); // otherFunction: 18.157958984375ms
Chrome的Console API畢竟不是標(biāo)準(zhǔn)的,除了使用它之外,還可以選擇日期插件 Date 中的 getTime now 相關(guān)方法
然而使用Date對(duì)象來(lái)計(jì)算耗時(shí)并不正統(tǒng),推薦使用標(biāo)準(zhǔn)的 performance.now
var start = performance.now(); console.time(); for (var i = 0; i < 10000000; ++i) { } var end = performance.now(); console.timeEnd(); // default: 23.598876953125ms console.log(end - start); // 23.600000015459955
可以看到,它們是相差不大的
使用類似的方法,將它包裝起來(lái)以便方便調(diào)用
var getFunExecTime = (function() { // 裝飾器,在當(dāng)前函數(shù)執(zhí)行前先執(zhí)行另一個(gè)函數(shù) function decoratorBefore(fn, beforeFn) { return function() { var ret = beforeFn.apply(this, arguments); // 在前一個(gè)函數(shù)中判斷,不需要執(zhí)行當(dāng)前函數(shù) if (ret !== false) { fn.apply(this, arguments); } }; } // 裝飾器,在當(dāng)前函數(shù)執(zhí)行后執(zhí)行另一個(gè)函數(shù) function decoratorAfter(fn, afterFn) { return function() { fn.apply(this, arguments); afterFn.apply(this, arguments); }; } // 執(zhí)行次數(shù) var funTimes = {}; // 給fun添加裝飾器,fun執(zhí)行前后計(jì)時(shí) return function(fun, funName) { funName = funName || fun; if (funTimes[funName]) { return funTimes[funName]; } // 綁定 funTimes[funName] = decoratorAfter(decoratorBefore(fun, function() { // 執(zhí)行前 funTimes[funName].timestampStart = performance.now(); }), function() { // 執(zhí)行后 funTimes[funName].timestampEnd = performance.now(); // 將執(zhí)行耗時(shí)存入 funTimes[funName].valueOf = function() { return this.timestampEnd - this.timestampStart; }; }); return funTimes[funName]; } })();
function someFunction() { for (var i = 0; i < 100000; ++i) { } } function otherFunction() { for (var i = 0; i < 10000000; ++i) { } } // 包裝 someFunction = getFunExecTime(someFunction); // 執(zhí)行 someFunction(); // 獲取耗時(shí),可直接使用函數(shù)的 valueOf console.log(+someFunction); // 2.0999999847263098 otherFunction = getFunExecTime(otherFunction, 'otherFunction'); otherFunction(); console.log(+otherFunction); // 21.00000000745058
三、如何控制函數(shù)的調(diào)用次數(shù)
也可以通過(guò)閉包來(lái)控制函數(shù)的執(zhí)行次數(shù)
function someFunction() { console.log(1); } function otherFunction() { console.log(2); } function setFunCallMaxTimes(fun, times, nextFun) { return function() { if (times-- > 0) { // 執(zhí)行函數(shù) return fun.apply(this, arguments); } else if (nextFun && typeof nextFun === 'function') { // 執(zhí)行下一個(gè)函數(shù) return nextFun.apply(this, arguments); } }; } var fun = setFunCallMaxTimes(someFunction, 3, otherFunction); fun(); // 1 fun(); // 1 fun(); // 1 fun(); // 2 fun(); // 2
四、如何控制函數(shù)的執(zhí)行時(shí)間
因?yàn)镴S是單線程的,控制函數(shù)的執(zhí)行時(shí)間相對(duì)來(lái)說(shuō)挺麻煩
通過(guò) async await yield 等異步特性,也許還是能辦到的
在React 16中的 Fiber 機(jī)制,在某種意義上是能控制函數(shù)的執(zhí)行時(shí)機(jī),得空再去看看它是怎么實(shí)現(xiàn)的吧
相關(guān)文章
Bootstrap 3的box-sizing樣式導(dǎo)致UEditor控件的圖片無(wú)法正??s放的解決方案
這篇文章主要介紹了Bootstrap 3的box-sizing樣式導(dǎo)致UEditor控件的圖片無(wú)法正常縮放的解決方案的相關(guān)資料,需要的朋友可以參考下2016-09-09詳解JavaScript中Math內(nèi)置對(duì)象基本方法的使用
Math?是javaScript的內(nèi)置對(duì)象,包含了部分?jǐn)?shù)學(xué)常數(shù)屬性和數(shù)學(xué)函數(shù)方法。本文將詳細(xì)講解Math基本方法的使用,感興趣的小伙伴可以學(xué)習(xí)一下2022-04-04JS獲取select-option-text_value的方法
這篇文章主要介紹了JS獲取select-option-text_value的方法,有需要的朋友可以參考一下2013-12-12js實(shí)現(xiàn)限定區(qū)域范圍拖拉拽效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)限定區(qū)域范圍拖拉拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11javascript 使用for循環(huán)時(shí)該注意的問(wèn)題-附問(wèn)題總結(jié)
所謂for循環(huán)就是重復(fù)的執(zhí)行一段代碼,for循環(huán)也是希望在創(chuàng)建循環(huán)時(shí)常會(huì)用到的工具,這篇內(nèi)容主要給大家介紹javascript 使用for循環(huán)時(shí)該注意的問(wèn)題-附問(wèn)題總結(jié),需要的朋友可以參考下2015-08-08js輪播圖透明度切換(帶上下頁(yè)和底部圓點(diǎn)切換)
本篇文章主要介紹了js輪播圖透明度切換(帶上下頁(yè)和底部圓點(diǎn)切換)的實(shí)例。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04javascript數(shù)組中的reduce方法和pop方法
這篇文章主要介紹了javascript數(shù)組中的reduce方法和pop方法,文章內(nèi)容介紹詳細(xì),具有一定的參考價(jià)值需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03