js 函數(shù)性能比較方法
在學(xué)習(xí)js過程中,經(jīng)常會(huì)遇到同樣一個(gè)功能點(diǎn) 這樣實(shí)現(xiàn)也可以,那樣實(shí)現(xiàn)也可以。但是哪個(gè)方式最優(yōu)呢?自己寫了一個(gè)簡(jiǎn)短的proferencesCompare 函數(shù)。代碼如下:
/** * 函數(shù)性能比較 * @param fns 要比較的函數(shù)數(shù)組 * @args 每個(gè)要比較函數(shù)在執(zhí)行的時(shí)候傳入的參數(shù),可以是數(shù)組,或者 被調(diào)用后 返回?cái)?shù)組類型 * @repeatCount 每個(gè)函數(shù)重復(fù)執(zhí)行的次數(shù),多次執(zhí)行 拉開差距。默認(rèn)值10000 * * @return [{runTime:執(zhí)行repeatCount次總時(shí)間,repeatCount:重復(fù)執(zhí)行次數(shù),name:函數(shù)名稱,chrome是函數(shù)名,IE由于不支持funciton.name,所以默認(rèn) fn+函數(shù)在fns中index}] * */function proferencesCompare(fns, args, repeatCount) {var tmpArgs, tmpFns;var result = [];var starTime, endTime;var i = 0;var repeatCount = repeatCount || 10000;var isFunction = false;if(fns === undefined) {throw Error(’Must have the compare funciton’);} var typeName = typeof args; //檢測(cè)傳入的args是否能夠返回array類型數(shù)據(jù)if(typeName === ’function’) {tmpArgs = args();isFunction = true;} if(typeName === ’number’) {tmpArgs = [];repeatCount = args;} //檢測(cè)args 是否為 arrayif(Object.prototype.toString.call(tmpArgs) !== ’[object Array]’) {throw Error(’The test args is must a array or a function which return the array’);} var len = fns.length;for(; i < len; i++) {var fnName = fns[i].name || 'fn' + i;starTime = Date.now();console.time(fnName);for(var j = 0; j < repeatCount; j++) {if(isFunction && (i !== 0 || j !== 0)) {//如果args是函數(shù),并且循環(huán)是第一次進(jìn)入,則不需要再執(zhí)行一次。前面做args檢測(cè)時(shí)已經(jīng)執(zhí)行過一次tmpArgs = args();}fns[i].apply(this, tmpArgs);}console.timeEnd(fnName);endTime = Date.now();result.push({ runTime: endTime - starTime, repeatCount: repeatCount, name: fnName });}return result;}
使用例子如下:
var fn1 = function() {var a;return !a;} var fn2 = function() {var a;return a === undefined;} var fn3 = function() {var a;return a == undefined;} var result = proferencesCompare([fn1, fn2, fn3, fn3, fn2, fn1], 1000000000);
這個(gè)例子主要比較 對(duì)于函數(shù)中 判斷對(duì)象是否為undefined 的幾種實(shí)現(xiàn)方式的性能比較。
chrome:
結(jié)果顯示 其實(shí)性能差不多。
下面是其他同學(xué)的補(bǔ)充
快速比較代碼執(zhí)行效率的方法
測(cè)試效率可以使用Stopwatch :
Stopwatch sw = new Stopwatch();sw.Start();//寫在要執(zhí)行的代碼前面
sw.Stop();//寫在要執(zhí)行的代碼結(jié)尾sw.Elapsed//得到代碼執(zhí)行時(shí)間
核心函數(shù)
int[] array = { 15,20,10,3,5};Stopwatch sw = new Stopwatch();sw.Start();for (int i = 0; i < array.Length - 1; i++) { for (int j = i + 1; j < array.Length; j++) { if (array[i] > array[j]) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } }}sw.Stop();Console.WriteLine(sw.Elapsed);
到此這篇關(guān)于js 函數(shù)性能比較方法的文章就介紹到這了,更多相關(guān)js 函數(shù)性能內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
