Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery do any initial processing?

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

like image 365
spender Avatar asked Sep 24 '09 23:09

spender


2 Answers

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.

like image 188
Crescent Fresh Avatar answered Nov 05 '22 16:11

Crescent Fresh


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

like image 38
lod3n Avatar answered Nov 05 '22 17:11

lod3n