Created
April 17, 2012 22:20
-
-
Save dontangg/2409438 to your computer and use it in GitHub Desktop.
Simple JavaScript performance tester
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
/** | |
* This is a simple function that will test the performance of a JavaScript function. | |
* It should be noted that it may not be extremely accurate because it depends on your | |
* JavaScript runtime's implementation of getTime(). Most of the time, it works great. | |
* | |
* (For more on performance testing, see http://ejohn.org/blog/accuracy-of-javascript-time/) | |
* | |
* Here is an example of how to run a performance test: | |
* var result = pfTest(function () { var testAssignment = 0; }, 50); | |
* console.log(result); | |
*/ | |
var pfTest = function (f, times) { | |
times = times || 1000; | |
var start = (new Date).getTime(); | |
for (var i = 0; i < times; i++) f(); | |
var end = (new Date).getTime(); | |
return ((end - start) / times) + "ms"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment