6

The browser console has a count method

 2 years ago
source link: https://christianheilmann.com/2022/07/14/the-browser-console-has-a-count-method/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The browser console has a count method

Thursday, July 14th, 2022 at 10:30 pm

Count von count from Sesame Street

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK