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?
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.
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.
Open the DevTools main menu. Select More tools > JavaScript Profiler. The old profiler opens in a new panel called JavaScript Profiler.
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.
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();
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With