The title of this post says it all.
I made a MINI script timer object that counts how long a certain piece of code runs (executes).
It’s very handy for optimizing youre code!
The output is displayed in seconds with 3 digits behind the comma.
if you just want milliseconds remove the “/1000″ from:
this.scriptRunTime = (this.stopTime - this.startTime)/1000;
The code:
/**
* javascript script timer
* example:
* scriptTimer.startTimer();
* alert('hello');
* scriptTimer.stopTimer();
* alert(scriptTimer.scriptRunTime);
*
*/
var scriptTimer = {
scriptRunTime : 0,
startTime : 0,
stopTime : 0,
startTimer : function(){
time = new Date();
this.startTime = time.getTime();
},
stopTimer : function(){
time = new Date();
this.stopTime = time.getTime();
this.scriptRunTime = (this.stopTime - this.startTime)/1000;
}
}