views:

119

answers:

3

Simple question. If you include jQuery in a page of HTML, is there any initialization overhead before one uses any jQuery functions.

+1  A: 

Yes, a tiny bit. On my very fast machine, it seems to delay the page load by about 4ms.

lod3n
+1  A: 

jQuery itself doesn't really do much to speak of at load time, but naturally downloading the script, parsing it and executing the inline code to define all its functions takes a little while. It's unlikely to be terribly significant in itself on a normal desktop browser.

bobince
+11  A: 

By virtue of simply including the jQuery script, you do indeed get some overhead. jQuery builds itself up inside an immediately executed function.

In 1.3.2 the biggest things it does are for IE support:

  • creates a temporary form element with one input element inside it, to check to see if the browser returns elements by name when querying by getElementById - [Source]
  • creates a temporary div with a empty comment node in it, to check to see if the browser returns only elements when doing getElementsByTagName("*") - [Source]
  • creates a temporary anchor element, to see if getAttribute returns normalized href attributes - [Source]
  • creates a temporary div with this html in it:

    '   <link/><table></table><a href="/a" style="color:red;float:left;opacity:.5;">a</a><select><option>text</option></select><object><param/></object>'
    

    and proceeds to read off a bunch of characteristics from that structure. This is to build the jQuery.support object that was created in lieu of deprecating jQuery.browser - [Source]

It also does some smaller things like:

  • creates a whole whack of regular expression objects
  • parses navigator.userAgent for some deprecated browser sniffing support
  • gets the current Date from the system (+new Date)

Keep in mind all this barely adds up to any noticeable lag, as others have suggested.

Crescent Fresh
Yes, feature tests are not very time-consuming, contrary to many beliefs. ~80 more-or-less involved tests in my CFT (http://yura.thinkweb2.com/cft/) only take ~18ms on FF3.5
kangax
@kangax: Holy hell. That is a fantastic reference.
Crescent Fresh