Home > Web Front-end > JS Tutorial > How to count the number of function executions in javascript? (detailed explanation)

How to count the number of function executions in javascript? (detailed explanation)

不言
Release: 2018-10-27 14:14:18
forward
4592 people have browsed it

The content of this article is about how to count the number of function executions in JavaScript? (Detailed explanation) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Count the number of function executions

The conventional method can be to use the console.log output to calculate how many outputs there are with the naked eye

But in Chrome There is a built-in console.count method that can count the number of times a string is output. We can use this to indirectly count the number of executions of the function

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
Copy after login

Without parameters, it is the default value, otherwise the number of executions of the string will be output, which is quite convenient to observe

Of course, in addition to outputting the number of times, if you also want to obtain a pure number of times, you can wrap the function with a decorator and use the object to store the number of calls internally

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
Copy after login

How to control the number of function calls

You can also use closures to control the number of executions of the function

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
Copy after login

The above is the detailed content of How to count the number of function executions in javascript? (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template