Last active
August 29, 2015 14:02
-
-
Save torgeir/eb8f62b5c83c878ac8cf to your computer and use it in GitHub Desktop.
es6 proxy profile method example
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
function profile (o) { | |
for (var key in o) { | |
var field = o[key]; | |
if (typeof field == 'function') { | |
o[key] = Proxy.createFunction({}, | |
// call | |
function () { | |
var before = new Date; | |
var res = field.apply(o, arguments); | |
var spent = new Date - before; | |
console.log('time spent calling "%s": %sms', key, spent); | |
return res; | |
}, | |
// new | |
function () { | |
}); | |
} | |
} | |
} | |
var o = { | |
doit: function () { | |
var i = 100000000; | |
while (i--); | |
return 42; | |
} | |
} | |
profile(o); | |
console.log(o.doit()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment