Created
January 4, 2012 16:26
-
-
Save ryanflorence/1560796 to your computer and use it in GitHub Desktop.
Log the time of JS operations
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
var logtime = (function() { | |
var ids = {}; | |
return function(id) { | |
if (!ids[id]) { | |
ids[id] = +new Date(); | |
return; | |
} | |
var time = +new Date() - ids[id]; | |
delete ids[id]; | |
console.log(id + ': ' + time + 'ms'); | |
return time; | |
} | |
}()); |
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
logtime('foo'); | |
doSomethingExpensive(); | |
logtime('foo'); // console logs the time between calls to logtime('foo') |
you can also use console.profile()
and console.profileEnd()
for a more detailed report on what inner function calls are actually the ones taking those precious milliseconds
All well and good until you're in IE8. And coupled with some less-inspired code we're talking seconds, not milliseconds!
Extending console
for this stuff might be smarter, though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should accomplish the same thing without a custom function...