Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created December 17, 2014 05:22
Show Gist options
  • Select an option

  • Save lsongdev/da1a84ba13939efb10cc to your computer and use it in GitHub Desktop.

Select an option

Save lsongdev/da1a84ba13939efb10cc to your computer and use it in GitHub Desktop.
MethodProfiler
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