Last active
December 27, 2015 09:19
-
-
Save itayw/7303500 to your computer and use it in GitHub Desktop.
hook object functions and measure times
This file contains 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
//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