Rocco Balsamo
Rocco Balsamo

Reputation: 381

Start and Stop Profiling from Javascript in Chrome

I would love to be able to start and stop the CPU Profiler in the Chrome developer window by making a javascript call. Something like:

chrome.cpuprofiler.start();  
//do expensive operation  
chrome.cpuprofiler.stop();

Right now, the best I can do is:

Click "start profiling".  
//do expensive operation  
Click "stop profiling".  

Is there even a shortcut key for this?

Upvotes: 38

Views: 6368

Answers (1)

mwolfetech
mwolfetech

Reputation: 1546

You can!

An example:

   if (window.console && window.console.profile) {
     console.profile("label for profile");
     // insert code to profile here,
     // all function calls will be profiled
     console.profileEnd();
   }

It also works on Safari, and with Firebug in Firefox.

Note: You can't use profile to time code that does not make a function call: if your code above is simply a for loop then the profiler won't find anything to profile. Use console.time() and console.timeEnd() to benchmark pure loops or code that does not call a function.

Upvotes: 63

Related Questions