Skip to content

Instantly share code, notes, and snippets.

@itayw
Last active December 27, 2015 09:19
Show Gist options
  • Save itayw/7303500 to your computer and use it in GitHub Desktop.
Save itayw/7303500 to your computer and use it in GitHub Desktop.
hook object functions and measure times
//Simple prototype modifier to add console.time measure
// whenever a function is called.
//The hook will ignore any function starting with _ as treat
// as private.
Object.prototype.hookEvents = function (modifier) {
var obj = this;
var name, fn;
for (name in obj) {
fn = obj[name];
//Ignore any private functions
if (name.substring(0, 1) == '_')
continue;
if (typeof fn === 'function') {
obj[name] = (function (name, fn) {
var args = arguments;
return function () {
modifier.apply(this, args);
console.time('Function ' + name);
var result = fn.apply(this, arguments);
console.timeEnd('Function ' + name);
return result;
}
})(name, fn);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment