美文网首页成长日记前端全栈开发
在JS中统计函数执行次数

在JS中统计函数执行次数

作者: a333661d6d6e | 来源:发表于2018-10-26 17:31 被阅读0次

一、统计函数执行次数

常规的方法可以使用 console.log 输出来肉眼计算有多少个输出

不过在Chrome中内置了一个 console.count 方法,可以统计一个字符串输出的次数。我们可以利用这个来间接地统计函数的执行次数

function someFunction() {
 console.count('some 已经执行');
}
function otherFunction() {
 console.count('other 已经执行');
}
someFunction(); // some 已经执行: 1
someFunction(); // some 已经执行: 2
otherFunction(); // other 已经执行: 1
console.count(); // default: 1
console.count(); // default: 2

不带参数则为 default 值,否则将会输出该字符串的执行次数,观测起来还是挺方便的

当然,除了输出次数之外,还想获取一个纯粹的次数值,可以用装饰器将函数包装一下,内部使用对象存储调用次数即可

var getFunCallTimes = (function() {
 
 // 装饰器,在当前函数执行前先执行另一个函数
 function decoratorBefore(fn, beforeFn) {
 return function() {
 var ret = beforeFn.apply(this, arguments);
 // 在前一个函数中判断,不需要执行当前函数
 if (ret !== false) {
 fn.apply(this, arguments);
 }
 };
 }
 
 // 执行次数
 var funTimes = {};
 
 // 给fun添加装饰器,fun执行前将进行计数累加
 return function(fun, funName) {
 // 存储的key值
 funName = funName || fun;
 
 // 不重复绑定,有则返回
 if (funTimes[funName]) {
 return funTimes[funName];
 }
 
 // 绑定
 funTimes[funName] = decoratorBefore(fun, function() {
 // 计数累加
 funTimes[funName].callTimes++;
 console.log('count', funTimes[funName].callTimes);
 });
 
 // 定义函数的值为计数值(初始化)
 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

如何控制函数的调用次数

也可以通过闭包来控制函数的执行次数

function someFunction() {
 console.log(1);
}
function otherFunction() {
 console.log(2);
}
function setFunCallMaxTimes(fun, times, nextFun) {
 return function() {
 if (times-- > 0) {
 // 执行函数
 return fun.apply(this, arguments);
 } else if (nextFun && typeof nextFun === 'function') {
 // 执行下一个函数
 return nextFun.apply(this, arguments);
 }
 };
}
var fun = setFunCallMaxTimes(someFunction, 3, otherFunction);
fun(); // 1
fun(); // 1
fun(); // 1
fun(); // 2
fun(); // 2
全栈开发交流群:864305860.png

相关文章

  • 在JS中统计函数执行次数

    一、统计函数执行次数 常规的方法可以使用 console.log 输出来肉眼计算有多少个输出 不过在Chrome中...

  • Python性能优化Tips

    代码性能分析工具 pylint:不执行代码,静态分析。 profile:执行代码,统计各个函数的调用次数以及耗时。...

  • this汇总

    this汇总 在js中this代表当前函数执行的主体(谁执行的这个函数) this是谁和函数在哪定义以及在哪执行的...

  • 12-函数节流

    1.什么是函数节流[throttle]?函数节流也是优化高频率执行js代码的一种手段可以减少高频调用函数的执行次数...

  • 【JS】立即执行函数(IIFE)/函数声明/表达式解析

    立即执行函数(Immediately Invoked Function Expression,IIFE) 在JS中...

  • Java 中执行js脚本

    java 脚本相关的api在javax.script中 直接执行js函数: 2.执行js脚本文件 Test.js ...

  • 谈谈变量声明提升

    1 谈谈变量提升当执行 JS 代码时,会生成执行环境,只要代码不是写在函数中的,就是在全局执行环境中,函数中的代码...

  • iOS数据埋点统计方案(附Demo): 运行时Method Sw

    1. 场景需求 统计UIViewController加载次数 统计UIButton点击次数 统计自定义方法的执行 ...

  • Js立即执行函数

    js立即执行函数可以让你的函数在创建后立即执行,js立即执行函数模式是一种语法,可以让你的函数在定义后立即被执行,...

  • 前端经典面试题合集(一)

    1.谈谈变量提升 考察点:js基础知识,js执行机制,变量的提升答:执行js代码时,会生成执行环境,在函数中的代码...

网友评论

    本文标题:在JS中统计函数执行次数

    本文链接:https://www.haomeiwen.com/subject/xmrltqtx.html