Return to Snippet

Revision: 32146
at September 19, 2010 16:57 by Meander365


Initial Code
(function() {
	var log = [], index = {}, first, last;
	// Accumulate seconds for the specified message.
	// Each message string has its own total seconds.
	function add( message, seconds ) {
		var i = index[message];
		if( i == null ) {
			i = log.length;
			index[message] = i;
			log[i] = { message:message, seconds:0 };
		}
		
		log[i].seconds += seconds;
	}
	
	profiler = function( message, since ) {
		var now = +new Date;
		add( message, ( now - ( since || last ) ) / 1000 );
		return last = +new Date;
	}
		
	profiler.done = function( sel ) {
		profiler( 'total', first );
		$(sel).html(
			$.map( log, function( item ) {
				return( item.seconds.toFixed(3) + ': ' + item.message + '<br />');
			}).join('')
		);
	};
	
	first = last = +new Date;
	
})();

Initial URL

                                

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

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

Initial Title
jQuery Profiler

Initial Tags
jquery

Initial Language
JavaScript