Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created January 4, 2012 16:26
Show Gist options
  • Save ryanflorence/1560796 to your computer and use it in GitHub Desktop.
Save ryanflorence/1560796 to your computer and use it in GitHub Desktop.
Log the time of JS operations
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;
}
}());
logtime('foo');
doSomethingExpensive();
logtime('foo'); // console logs the time between calls to logtime('foo')
@ryanflorence
Copy link
Author

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