Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created January 4, 2012 16:26
Show Gist options
  • Select an option

  • Save ryanflorence/1560796 to your computer and use it in GitHub Desktop.

Select an option

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')
@paularmstrong
Copy link
Copy Markdown

This should accomplish the same thing without a custom function...

console.time('foo');
doSomethingExpensive();
console.timeEnd('foo');

@gonchuki
Copy link
Copy Markdown

gonchuki commented Jan 4, 2012

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

@ryanflorence
Copy link
Copy Markdown
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