Skip to content

Javascript count function calls in Chrome profiler

Is it possible to get information from Chrome profile (Developer Tools) how many times functions are called?
I found how to do it in code:

console.count("Function called");

and some really old (year 2011 and around) topics and feature request like this one. But no new information if it is possible to record count for every function calls.

Sometimes you figure it out that some calls are called many times in a second and would greatly improve performance if called only once (add some delay for execution). In order to track this functions counter is necessary.

Answer

You will not see function calls count in the timeline / CPU profiler since the standard profiler in Chrome Dev Tools is a sampling profiler.

A sampling profiler takes execution stack snapshots at a predefined interval. When it is about to do so, the JS execution is paused and functions on the current execution stack are recorded. This is what you see in the flame-chart of the timeline.

Given the described behaviour it should be clear that a sampling profiler can not record all function calls (a function could be invoked and finish its execution between 2 measuring pauses).

There are other profilers that can record all function calls, the easiest to use is probably the Web Tracing Framework. It works by instrumenting your code (rewrite it by wrapping each and every function call with measuring code). WTF takes a bit more time to setup (instrumentation step) and will have impact on times measured (as it injects new code) but at least can show all function calls.

The bottom line is that there is no single profiler that would be perfect for all tracing jobs. You need to use different ones depending on what you want to measure. There is an excellent talk that goes into details of different profilers, highly recommended: https://www.youtube.com/watch?v=nxXkquTPng8