views:

479

answers:

3

Hi,

I'd like to measure how long it takes to run the whole $().ready() scope in each of page.

For profiling specific functions I just set a new Date() variable at the beginning of the relevant part and then check how long it takes to get to the end of the relevant part.

The problem with measuring the whole $().ready scope is that it can sometimes run some of the code asynchronously and then I can not wait for it all to finish and see how long it has taken.

Is there any event which is fired once the page has completely finished running all $().ready code?

EDIT: Using Firebug or other client debuggers are not an option since I also need to collect this profiling information from website users for monitoring and graphing our web site's page load speeds

Thanks!

A: 

Have you tried using Profiler in Firebug?

RaYell
I need to collect statistics from users too so I could monitor page load speeds
Saggi Malachi
+1  A: 

Swap out the jQuery ready function with a function that does your start and finish tracking, and calls the original method.

jQuery.ready = (function() {
    var original = jQuery.ready;
    return function() {
     alert('starting profiler');
     original();
     alert('ending profiler');
    };
})();

$(function() {
    alert('this message will appear between the profiler messages above...');
});
great_llama
Don't forget to call the original with the same arguments.
SolutionYogi
I modified your code to pass not only the original arguments but the original scope as well.
SolutionYogi
Why? The ready function doesn't take any arguments. Might have been a good idea if this were a general solution, but it was coded entirely with jQuery's ready method in mind. May as well change it to bubble up the ready method's return value.... oh, wait, it doesn't return anything...
great_llama
Thanks but in my ready function I fire some functions that run asynchronously. is there anyway to register some callback event for when they are all done?
Saggi Malachi
I forgot to mention that using this solution would still alert 'ending profiler' before its finished running an asynchronous function I've fired up in between
Saggi Malachi
Ready function does take an argument, the function to be called when the document is ready. You can revert back the changes if you want, but this code change will make the your replacement future proof.
SolutionYogi
jQuery.ready != jQuery.fn.readyjQuery.ready is what runs through the readyList array when the document is finally loaded. jQuery.fn.ready is the one that gets called to queue up those functions (or call them if the document is already loaded). jQuery.ready doesn't accept any arguments.http://jqueryjs.googlecode.com/files/jquery-1.3.2.jsCompare lines 3018-3043 to lines 2957-2972.
great_llama
+1  A: 

There will be no event fired because its virtually impossible for ready() to know when any asynchronous functions are done processing. Thus, you'll need to bake this functionality in yourself; you could use jQuery's custom events, or perhaps set a function to run on setInterval() that can introspect the environment and deduce whether or not everything else is done.

ken