This content originally appeared on DEV Community and was authored by Christian Heilmann
When debugging or analysing JavaScript, you often see people trying to find out how often a certain function is called. The common way to do that is to use a global counter variable to increment and log in the function.
var i = 0;
function test(){
// other functionality
i++;
console.log(i);
// other functionality
}
There is, however, a better method. The Console of the browser has a count()
and countReset()
method that event takes a label. That means you can avoid the global.
function bettertest(){
console.count('bettertest');
}
You can see it in action in this screencast.
This is part of the standard Console API and should be supported in all browsers.
This content originally appeared on DEV Community and was authored by Christian Heilmann
