Created
December 17, 2014 05:22
-
-
Save lsongdev/da1a84ba13939efb10cc to your computer and use it in GitHub Desktop.
MethodProfiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var MethodProfiler = function(component) { | |
| this.component = component; | |
| this.timers = {}; | |
| this.log = document.createElement("ul"); | |
| var body = document.body; | |
| document.getElementById("result").appendChild(this.log); | |
| for(var key in this.component) { | |
| // Ensure that the property is a function. | |
| if(typeof this.component[key] !== 'function') { | |
| continue; | |
| } | |
| // Add the method. | |
| var that = this; | |
| (function(methodName) { | |
| that[methodName] = function() { | |
| that.startTimer(methodName); | |
| var returnValue = that.component[methodName].apply(that.component,arguments); | |
| that.displayTime(methodName, that.getElapsedTime(methodName)); | |
| return returnValue; | |
| }; | |
| })(key); } | |
| }; | |
| MethodProfiler.prototype = { | |
| startTimer: function(methodName) { | |
| this.timers[methodName] = (new Date()).getTime(); | |
| }, | |
| getElapsedTime: function(methodName) { | |
| return (new Date()).getTime() - this.timers[methodName]; | |
| }, | |
| displayTime: function(methodName, time) { | |
| var li = document.createElement("li"); | |
| var text = document.createTextNode(methodName + ': ' + time + ' ms'); | |
| li.appendChild(text); | |
| this.log.appendChild(li); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment