Skip to content

Instantly share code, notes, and snippets.

@NikkiLoveGod
Last active August 29, 2015 14:22
Show Gist options
  • Save NikkiLoveGod/2b98c3ebf7c7200b882f to your computer and use it in GitHub Desktop.
Save NikkiLoveGod/2b98c3ebf7c7200b882f to your computer and use it in GitHub Desktop.
Measure the performance of a javascript callable
/**
* Measures the runtime of given callable function
* Second argument is optional, and puts an identifier string in front of console.log
*/
function timePerformance(callable) {
// Identifier string to differentiate timings
var identifier = arguments[1] || 'Time';
// Start time
var t0 = performance.now();
// Run the function, log any results
var result = callable();
if(result) {
console.log(result);
}
// End time
var t1 = performance.now();
// Log out the timing
var runtime = (t1 - t0).toFixed(3);
console.log(identifier + ": " + runtime + " milliseconds");
return runtime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment