jQuery Profiler


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

Track how long code takes to load by using a cross browser profiler. Useage :

profiler("start");
//your js
profiler.done("#profiler");


Copy this code and paste it in your HTML
  1. (function() {
  2. var log = [], index = {}, first, last;
  3. // Accumulate seconds for the specified message.
  4. // Each message string has its own total seconds.
  5. function add( message, seconds ) {
  6. var i = index[message];
  7. if( i == null ) {
  8. i = log.length;
  9. index[message] = i;
  10. log[i] = { message:message, seconds:0 };
  11. }
  12.  
  13. log[i].seconds += seconds;
  14. }
  15.  
  16. profiler = function( message, since ) {
  17. var now = +new Date;
  18. add( message, ( now - ( since || last ) ) / 1000 );
  19. return last = +new Date;
  20. }
  21.  
  22. profiler.done = function( sel ) {
  23. profiler( 'total', first );
  24. $(sel).html(
  25. $.map( log, function( item ) {
  26. return( item.seconds.toFixed(3) + ': ' + item.message + '<br />');
  27. }).join('')
  28. );
  29. };
  30.  
  31. first = last = +new Date;
  32.  
  33. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.