Live data from Hacker News

Don’t let jQuery’s $(document).ready() slow you down

encosia.com

1–10 of 25 posts

Re: Don’t let jQuery’s $(document).ready() slow you down

#3

Important takeaway: Use Firebug's network monitoring to find out what actually happens when your page loads.

the rest seems to be summarised as: don't wait for things you don't need to wait for.

$(document).ready() can be a bit of a sledgehammer in terms of latency.

Re: Don’t let jQuery’s $(document).ready() slow you down

#4
I'm not sure he knows how .live works. .live doesn't wait for items to be ready and it only binds one event on to the top level document. .live waits for bubbled events and if the item bubbling the event matches this .live selector it calls back. Pretty smart if you think about it.

But either way he makes a great point on the document.ready if your page depends on ajax or some sort of ui styling its probably best in the header script.

Re: Don’t let jQuery’s $(document).ready() slow you down

#5

I'm not sure he knows how .live works. .live doesn't wait for items to be ready and it only binds one event on to the top level document. .live waits for bubbled events and if the item bubbling the event matches this .live selector it calls back. Pretty smart if you think about it. But either way he makes a great point on the document.ready if your page depends on ajax or some sort of ui styling its probably best in…

Right, that's the crux of my overall point about .live(). Since it's using event delegation, the DOM elements don't need to be present at the time you declare it. If you have the opportunity to set up your .live() handlers early, you should.

Re: Don’t let jQuery’s $(document).ready() slow you down

#6
post #5

I'm not sure he knows how .live works. .live doesn't wait for items to be ready and it only binds one event on to the top level document. .live waits for bubbled events and if the item bubbling the event matches this .live selector it calls back. Pretty smart if you think about it. But either way he makes a great point on the document.ready if your page depends on ajax or some sort of ui styling its probably best in…

Right, that's the crux of my overall point about .live(). Since it's using event delegation, the DOM elements don't need to be present at the time you declare it. If you have the opportunity to set up your .live() handlers early, you should.

[deleted]

Re: Don’t let jQuery’s $(document).ready() slow you down

#8
post #7

I know this is off topic, but there's another reason it slows you down - too many keystrokes. I prefer $(function(){}); to $(document).ready(function(){});

I'm torn when it comes to that. The readability of $(document).ready() is better. Especially for later devs who may not be familiar with jQuery.

Re: Don’t let jQuery’s $(document).ready() slow you down

#9
post #8
post #7

I know this is off topic, but there's another reason it slows you down - too many keystrokes. I prefer $(function(){}); to $(document).ready(function(){});

I'm torn when it comes to that. The readability of $(document).ready() is better. Especially for later devs who may not be familiar with jQuery.

For devs not familiar with jQuery (and on the off-chance that you'll need no-conflicts mode at some point), you might even avoid the dollar sign.

jQuery(document).ready(function(){});

Re: Don’t let jQuery’s $(document).ready() slow you down

#10
post #9
post #8

Earlier quoted context omitted.

I'm torn when it comes to that. The readability of $(document).ready() is better. Especially for later devs who may not be familiar with jQuery.

For devs not familiar with jQuery (and on the off-chance that you'll need no-conflicts mode at some point), you might even avoid the dollar sign. jQuery(document).ready(function(){});

Or even better:

  jQuery(document).ready(function($){});
Post reply on HN