When looking to improve a page's performance, one technique I haven't heard mentioned before is using setTimeout to prevent javascript from holding up the rendering of a page.

For example, imagine we have a particularly time-consuming piece of jQuery inline with the html:

$('input').click(function () {
    // Do stuff
});

If this code is inline, we are holding up the perceived completion of the page while the piece of jquery is busy attaching a click handler to every input on the page.

Would it be wise to spawn a new thread instead:

setTimeout(function() {
    $('input').click(function () {
        // Do stuff
    })
}, 100);

The only downside I can see is that there is now a greater chance the user clicks on an element before the click handler is attached. However, this risk may be acceptable and we have a degree of this risk anyway, even without setTimeout.

Am I right, or am I wrong?

Edit
Report