Skip to content

Instantly share code, notes, and snippets.

@binarymax
Forked from 140bytes/LICENSE.txt
Created May 22, 2011 23:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save binarymax/985992 to your computer and use it in GitHub Desktop.
Save binarymax/985992 to your computer and use it in GitHub Desktop.
Javascript Performance Timer
/*********************************
*
* 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
}
}
}
<!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>
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)}}}
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.
{
"name": "Nested Performance Timer",
"description": "Nicely nested timers for easy performance debugging",
"keywords": [
"Performance",
"Javascript",
"Test",
"Debugging"
]
}
@jed
Copy link

jed commented Jul 11, 2011

hey @binarymax, could you please remove the comments from this package.json?

@hartrock
Copy link

What about push/pop stack ops? Saves a few bytes...

@stuartbannerman
Copy link

function T()
{
var a = {};
return {
s : function(n)
{
a[n] = new Date();
},
e : function(n)
{
return new Date() - a[n];
}
}
}

function T(){var a={};return{s:function(n){a[n]=new Date();},e:function(n){return new Date()-a[n];}}}

101 characters :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment