Last active
August 29, 2015 14:22
-
-
Save NikkiLoveGod/2b98c3ebf7c7200b882f to your computer and use it in GitHub Desktop.
Measure the performance of a javascript callable
This file contains 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
/** | |
* 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