The browser console has a count method

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(){


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.

Screencast of the two ways to count how often a method was called in comparison

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


Print Share Comment Cite Upload Translate
APA
Christian Heilmann | Sciencx (2023-10-04T22:53:25+00:00) » The browser console has a count method. Retrieved from https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/.
MLA
" » The browser console has a count method." Christian Heilmann | Sciencx - Thursday July 14, 2022, https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/
HARVARD
Christian Heilmann | Sciencx Thursday July 14, 2022 » The browser console has a count method., viewed 2023-10-04T22:53:25+00:00,<https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/>
VANCOUVER
Christian Heilmann | Sciencx - » The browser console has a count method. [Internet]. [Accessed 2023-10-04T22:53:25+00:00]. Available from: https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/
CHICAGO
" » The browser console has a count method." Christian Heilmann | Sciencx - Accessed 2023-10-04T22:53:25+00:00. https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/
IEEE
" » The browser console has a count method." Christian Heilmann | Sciencx [Online]. Available: https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/. [Accessed: 2023-10-04T22:53:25+00:00]
rf:citation
» The browser console has a count method | Christian Heilmann | Sciencx | https://www.scien.cx/2022/07/14/the-browser-console-has-a-count-method/ | 2023-10-04T22:53:25+00:00
https://github.com/addpipe/simple-recorderjs-demo