Created
December 9, 2014 19:48
-
-
Save eoftedal/25c3b33ed1a8c56f16f5 to your computer and use it in GitHub Desktop.
Performance logging in Chrome
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
function perfLog(fun, context) { | |
var context = context || this; | |
var realFun = context[fun.name]; | |
context[fun.name] = function() { | |
console.time(fun.name); | |
var result = realFun.apply(context, arguments); | |
console.timeEnd(fun.name); | |
return result; | |
} | |
} | |
//How to use | |
perfLog(eval); | |
perfLog(document.body.appendChild, document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool idea to do logging on existing methods and native APIs! This AOP-ish way of doing it can also easily be extended to have a debug-flag or regex-pattern where can filter out different log-statments to easier drill down on specific log-messages (all though you can have this functionality in for instance Developer Tools in Chrome).
In many cases, though, in the current JS implementation this wouldn't give expected result. Actually, you might end up overriding a lot of methods and re-naming them as empty string. Let's say we have an object literal:
The name of
o.foo
in this case would be""
(empty string). This is due to the function being name-less (anonymous). Doing this, however, would work:Never fear, ECMAScript 6 to the rescue! In the new spec this is handled much better, as
name
is much more formalised (see this article: http://bocoup.com/weblog/whats-in-a-function-name/). This means that the code above would work as expected, and even:Until then it might be a good idea to add a warning to the function:
That being said, this is probably not a side-effect you would want in production code, as it can affect things way outside your own code base and third party code. 😄
Maybe a more boring approach, but yet traditional:
This might not look as smooth, but it works with anonymous methods/functions 👔