Function Timing Example (includes jQuery)


/ Published in: jQuery
Save to your folder(s)

A way to time how long it takes to get through a certain block of code. Good way to measure performance on the page.

You could also use the built in logging functions in firebug. Not sure how they would tally up in a test.


Copy this code and paste it in your HTML
  1. $(function(){
  2. console.info("Start Test");
  3. var d = new Date();//Get our start time
  4. console.info(d.getSeconds() + " " + d.getMilliseconds());
  5.  
  6. //Run our test
  7. var testBody = "";
  8. for (var i=0; i<1000; i++){
  9. testBody += "<div class='testable"+i+"'>";
  10. }
  11. $("body").append(testBody);
  12. for (var j=0; j<1000; j++){
  13. $(".testable"+j);
  14. }
  15.  
  16. var d = new Date();//Get our end time
  17. console.info(d.getSeconds() + " " + d.getMilliseconds());
  18. console.info("End Test");
  19. /**
  20. * Console will now log 2 times, the difference between
  21. * them is how long the test took to run
  22. */
  23. });

URL: http://is.gd/1mhJr

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.