Profiler: Object Profiling Example
This example shows using the Profiler on all methods in an object. It uses the Y.DOM
object
as the object to be profiled.
Object Profiling Example
To illustrate using the Profiler on objects, the Y.DOM
and Y.Node
objects are registered for profiling. This means
that all of the methods on these objects are being profiled. To
illustrate their use, a number of demo elements are added to the markup:
<div class="bar">div class="bar"</div> <div class="bar-baz">div class="bar-baz"</div> <div class="bar ">div class="bar "</div> <div class=" bar ">div class=" bar "</div> <div class="bar baz">div class=" bar baz"</div> <div class="bar2 baz">div class=" bar2 baz"</div> <div class="foo">div class="foo"</div> <div class="foo" id="bar">div class="foo" id="bar"</div> <div class="foo bar baz">div class="foo bar baz"</div> <p class="bar">p class="bar"</p> <button id="demo-run">run</button>
<div class="bar">div class="bar"</div> <div class="bar-baz">div class="bar-baz"</div> <div class="bar ">div class="bar "</div> <div class=" bar ">div class=" bar "</div> <div class="bar baz">div class=" bar baz"</div> <div class="bar2 baz">div class=" bar2 baz"</div> <div class="foo">div class="foo"</div> <div class="foo" id="bar">div class="foo" id="bar"</div> <div class="foo bar baz">div class="foo bar baz"</div> <p class="bar">p class="bar"</p> <button id="demo-run">run</button>
The button is used to run the example. The function being called when the button is clicked is assigned by first
retrieving a Node
instance for the button and then using the on
method:
<script type="text/javascript"> YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {profiler:true, example:true}}).use(("node", "profiler"), function (Y) { Y.Node.get('#demo-run').on('click', function(){ Y.Profiler.registerObject("Y.Node", Y.Node); Y.Profiler.registerObject("Y.DOM", Y.DOM); var results = Y.Node.all('.bar'); results.addClass("newclass"); var report = Y.Profiler.getFullReport(function(data){ return data.calls > 0; }); Y.Profiler.unregisterObject("Y.Node"); Y.Profiler.unregisterObject("Y.DOM"); //output results var msg = ""; for (var func in report){ msg += (func + "(): Called " + report[func].calls + " times. Avg: " + report[func].avg + ", Min: " + report[func].min + ", Max: " + report[func].max) + " "; } alert(msg); }); }); </script>
<script type="text/javascript"> YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {profiler:true, example:true}}).use(("node", "profiler"), function (Y) { Y.Node.get('#demo-run').on('click', function(){ Y.Profiler.registerObject("Y.Node", Y.Node); Y.Profiler.registerObject("Y.DOM", Y.DOM); var results = Y.Node.all('.bar'); results.addClass("newclass"); var report = Y.Profiler.getFullReport(function(data){ return data.calls > 0; }); Y.Profiler.unregisterObject("Y.Node"); Y.Profiler.unregisterObject("Y.DOM"); //output results var msg = ""; for (var func in report){ msg += (func + "(): Called " + report[func].calls + " times. Avg: " + report[func].avg + ", Min: " + report[func].min + ", Max: " + report[func].max) + " "; } alert(msg); }); }); </script>
The function begins be registering Y.DOM
and Y.Node
with the Profiler. Note that since these objects don't
exist in the global scope, the second argument is necessary for registerObject()
. Then, the Y.Node.all()
method is called to retrieve nodes for each element with a class of bar
. The result of this operation is a NodeList
object, on which the addClass()
method is called. After that, the full report is returned and the objects are unregistered. The last
step is to output all of the information that was collected. Even though there's only two methods called explicitly in this example,
the profiled data indicates that several other methods on Y.DOM
and Y.Node
were called internally to accomplish the
tasks.