Javascript : Find script execution time

Using the script below you can find how much time it took to execute your javascript code.

var startDate = new Date();

// execute your tasks here

var endDate = new Date();

var timeTaken = endDate.getTime() - startDate.getTime();

alert('Time take to execute the script is '+timeTaken+' milliseconds');

You can try this example


// USE OF THIS FUNCTION IS NOT RECOMMENDED, CONSUMES LOT OF SYSTEM RESOURCES
function sleep(milliSeconds){
	var startTime = new Date().getTime(); // get the current time
	while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

var startDate = new Date();

// execute your tasks here
sleep(300); // parameter is milliseconds

var endDate = new Date();
var timeTaken = endDate.getTime() - startDate.getTime();

alert('Time take to execute the script is '+timeTaken+' milliseconds');

sleep function taken from http://www.devcheater.com/#JavaScript sleep by loop. Usage of this sleep function is not recommended, shown only for demo purpose.

Check PHP version here

This entry was posted in JavaScript, Web development and tagged , . Bookmark the permalink.

4 Responses to Javascript : Find script execution time

  1. Pingback: PHP : Find script execution time | eInternals

  2. very cool & good JS tips, thank you very much for sharing.

  3. Pingback: ActionScript : Find code execution time | eInternalseInternals

  4. onlined says:

    I have learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how much effort you set to create this type of fantastic informative web site.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>