Javascript count function calls in Chrome profiler Javascript count function calls in Chrome profiler google-chrome google-chrome

Javascript count function calls in Chrome profiler


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


I created a code to call a method with custom name so then you can lookup for it in flame charts and just count how many times was it called

export const traceToFlameChart = (param: string): void => {  const name = 'trace' + param;  const code = `function ${name}() {console.log('running ${param}')}; ${name}()`;  eval(code);};