Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

profiling anonymous javascript functions (chrome)

when performance profiling in chrome anonymous high-use functions are difficult to trouble-shoot when they are listed at the root of the call tree. is there a way to determine where an anonymous function was first instantiated?

like image 660
Paul Avatar asked May 30 '14 19:05

Paul


People also ask

How do I use DevTools profiler on Chrome?

To access the Performance tab, navigate to the website you want to profile, then open Chrome DevTools by right-clicking and selecting Inspect. Select the Performance tab inside Chrome DevTools. The easiest way to capture a performance profile is by clicking the Start profiling and reload page icon.

What is profiler in Chrome?

Profilers show us which functions take the most time. Let's make our baseline profile by switching to the “Profiles” tab in Chrome Developer Tools, where three types of profiling are offered: JavaScript CPU profile Shows how much CPU time our JavaScript is taking.

How do I profile in JavaScript?

Open the DevTools main menu. Select More tools > JavaScript Profiler. The old profiler opens in a new panel called JavaScript Profiler.

How do you execute an anonymous function?

Another use case of anonymous functions is to invoke the function immediately after initialization, this is also known as Self Executing Function. This can be done by adding parenthesis we can immediately execute the anonymous function.


1 Answers

You can utilize console.profile([label]), console.profileEnd(), console.time([label]), console.timeEnd([label]).

For example, execute the below in the JS snippet in console then review the anonynous function execution profile under 'Customize and control DevTools > More tools > JavaScript Profile'.

console.profile("anonymous function");
console.time("anonymous function");
(function() {
  var a = 123;
  function abc() {
    return a
  }
  abc();
}());
console.timeEnd("anonymous function");
console.profileEnd();

enter image description here

TIP: Storing JS in DevTools Snipets: Select Sources -> Snippets -> right-click -> select New -> place javascript within middle panel -> highlight / select text of javascript within middle panel -> right-click -> select Evaluate in Console -> click right-triangle at right panel or Ctrl+Enter.

like image 155
guest271314 Avatar answered Sep 19 '22 16:09

guest271314