Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any downsides to including the jquery library on my page?

I have been using javascript for some while now and recently began using jquery which I will admit I am fan of. <script type='text/javascript' src='../locationOfJquery/jquery.js'></script> allows use of the library in the script tags on that page. What I want to know is if just including the script tags slows down page load time any, even if there is no jquery code on the page, and also if there are any other major downsides to using jquery

like image 797
John Avatar asked Jul 25 '12 19:07

John


2 Answers

  • Put the script tags at the bottom of the page. This will not slow down processing of the DOM before onload events fire.
  • Use the minified version of jQuery, which is about as small as a small image/icon.
  • If visitors visit more than one page in your site, it will also usually be cached after their first visit. It may also already be pre-cached (or served from a more-local server) if you use a content delivery network (e.g. Google's). Good first impressions are critical.

To further answer smaller questions you had:

  • If there is no jQuery code on the page, jQuery must still be parsed. You can see how long it takes your computer to parse jQuery by using a profiling tool such as Chrome's.
  • There are frameworks which optimize your javascript on a per-page basis, but those have to trade off the ability to cache a script versus the gains in faster parsing. You almost certainly shouldn't worry about them. jQuery is very lightweight compared to other frameworks.

Numbers:

For example on Chrome when loading the Stackoverflow website, requesting the jQuery library from the Google CDN, the results were:

  • 0.027ms aggregate time spent download jQuery (perhaps cached)
  • 35.992ms aggregate time spent evaluating jQuery and performing any default DOM/CSS operations

This is all relative of course. I bet when you loaded this page you did not notice any lag because the entire page took about 630ms to load.

like image 98
ninjagecko Avatar answered Nov 11 '22 04:11

ninjagecko


The client will have to download the jQuery script (which is quite small). To further optimize you can just use hosted "Content Delivery Network" versions from Google or Microsoft. Also remember to use the minified version which downloads faster.

This article states the reasons why.

like image 20
Anthony Accioly Avatar answered Nov 11 '22 02:11

Anthony Accioly