-
-
Save chrisevans/1126987 to your computer and use it in GitHub Desktop.
Javascript Performance Timer
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
/********************************* | |
* | |
* Javascript performance timer | |
* | |
* Nicely nested timer operations | |
* for easy performance debugging | |
* | |
*********************************/ | |
function T(){ | |
var a=[]; //Holds the timer stack | |
return{ | |
s:function(n){ //Starts a new timer, pass in 'n' as the name of the timer | |
a.unshift({n:n,t:new Date()}) //Puts the timer on top of the stack | |
}, | |
e:function(l){ //Ends the most recently started timer | |
l=a.shift(); //Gets the timer from top of the stack | |
return((new Date()-l.t)+'ms|'+l.n) //Returns elapsed milliseconds of named timer | |
} | |
} | |
} |
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
<!doctype html> | |
<html> | |
<head> | |
<title>JS Performance Timer</title> | |
<meta charset="utf-8" /> | |
<script src="index.js" type="text/javascript"></script> | |
<script language="javascript" type="text/javascript"> | |
function page_load() { | |
var Timer = T(); | |
Timer.s("Timer Test"); | |
var innocuous = 0; | |
Timer.s("Some large operation"); | |
for(var i=0,l=100,k=0;i<l;i+=0.001) k=(Math.sin(Math.pow(i,2))); | |
Timer.s("Some small operation"); | |
innocuous++; | |
console.log(Timer.e()); | |
console.log(Timer.e()); | |
console.log(Timer.e()); | |
} | |
</script> | |
</head> | |
<body onload="page_load();"> | |
</body> | |
</html> |
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
function T(){var a=[];return{s:function(n){a.unshift({n:n,t:new Date()})},e:function(l){l=a.shift();return((new Date()-l.t)+'ms|'+l.n)}}} |
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
Copyright (c) 2011 Max Lovenheim Irwin, http://binarymax.com | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
{ | |
"name": "Nested Performance Timer", | |
"description": "Nicely nested timers for easy performance debugging", | |
"keywords": [ | |
"Performance", | |
"Javascript", | |
"Test", | |
"Debugging" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment