YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Profiler: Object Profiling Example

# Profiler: Object Profiling Example

This example shows using the Profiler on all methods in an object. It uses the `YAHOO.util.Dom` object as the object to be profiled.

### Note:

Keep an eye on the Logger console at right to see the profiling information being reported.

div class="bar"
div class="bar-baz"
div class="bar "
div class=" bar "
div class=" bar baz"
div class=" bar2 baz"
div class="foo"
div class="foo" id="bar"
div class="foo bar baz"

p class="bar"

## Object Profiling Example

To illustrate using the Profiler on objects, the `YAHOO.util.Dom` object is registered for profiling. This means that all of the methods on the object are being profiled. The two methods being called explicity are `getElementsByClassName()` and `addClass()`, although the Profiler will show that these methods use other methods on `YAHOO.util.Dom`. 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>

The button is used to run the example. The function being called when the button is clicked is assigned using the YUI Event Utility's `on()` method:

<script type="text/javascript"> YAHOO.util.Event.on('demo-run', 'click', function(){ YAHOO.tool.Profiler.registerObject("YAHOO.util.Dom"); var results = YAHOO.util.Dom.getElementsByClassName('bar'); YAHOO.util.Dom.addClass(results, "newclass"); var report = YAHOO.tool.Profiler.getFullReport(function(data){ return data.calls > 0; }); YAHOO.tool.Profiler.unregisterObject("YAHOO.util.Dom"); //output results for (var func in report){ YAHOO.log(func + "(): Called " + report[func].calls + " times. Avg: " + report[func].avg + ", Min: " + report[func].min + ", Max: " + report[func].max); } }); </script>

The function begins be registering `YAHOO.util.Dom` with the Profiler. Note that since this object exists in the global scope, only one argument is necessary for `registerObject()`. Then, the `getElementsByClassName()` and `addClass()` methods are called. After that, the full report is returned and the object is 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 `YAHOO.util.Dom` were called internally to accomplish the tasks.

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

### Profiler Examples:

- Simple Profiling Example
- Object Profiling Example

### More Profiler Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings