1

How could I run a simple diagnostic to check how many times a particular line of code is executed (passed by) in Chrome Dev Tools? Obviously without counting it myself with a debugging breakpoint and without writing any redundant code inside the codebase.

I want to do it on a regular basis, that is - I do something on the interface to call for a for (f.ex) cycle and I want to see imediately how many times a particular line was executed, then press again and see a new result again (maybe a reset inbetween could be tolerated)

1
  • 1
    Maybe with console.count() ? I don't know exactly if this is what you are looking for Commented Sep 5, 2016 at 22:05

1 Answer 1

3

You could use console.count, it will log how many times it was called with a given label:

console.count('Some Identifier')

If you don't want your log filled up with "foo: 19" and just want a total instead, you could create your own count function that only returns the total when requested:

var counter = (function() {
  var counters = {};

  return {
    count: function(label) {
      counters[label] = counters[label] ? counters[label] + 1 : 1;
      return this;
    },
    total: function(label) {
      return +counters[label];
    }
  }
})();

for (var i = 0; i < 10; i++) { 
  counter.count('bar'); 
}

console.log("bar called", counter.total('bar'), "times");

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, is there a way to do this with some Chrome Dev Tools extension? that is, without inserting code manually. Would probably be convenient.
console.count is the built-in, I just sometimes find the output to be a bit much when I only really care about the total
But you still have to insert console.count into the code, I mean - is there a way to do it with GUI? Like with breakpoints

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.