Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 394
Rocco Balsamo Avatar asked Mar 17 '11 22:03

Rocco Balsamo


People also ask

What is JavaScript profiling?

A JS profiler is an efficient tool to help you understand your code better – effectively finding, pinpointing and optimizing bottlenecks in your code. They're simple to run once you get used to the interface and it's likely you even have one built into your browser.

How do I use Chrome profiling?

To profile a page load, click Record Page Load. DevTools automatically starts the recording and then automatically stops when it detects that the page has finished loading. To profile a running page, click Record, perform the actions that you want to profile, and then click Stop when you're finished.

What does scripting mean in chrome performance?

To be able to visually identify what was going on when you profiled your website, Timeline is painted in different colors. JavaScript execution time is marked in yellow and it's called Scripting. The purple area shows the Rendering and the green parts of the Timeline show the Painting process.


1 Answers

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.

like image 68
mwolfetech Avatar answered Sep 19 '22 10:09

mwolfetech