Hao
Hao

Reputation: 6599

Will loading jQuery on individual pages be helping with overall website pages load time?

If jQuery is only needed on certain pages, will loading jQuery on these individual pages be helpful with overall website pages load time?

Given browser will cache javascript, it won't make much difference. But overall speaking, will it? Or at least it doesn't hurt to do that. Right?

Upvotes: 0

Views: 36

Answers (2)

Denys Séguret
Denys Séguret

Reputation: 382150

You should never import a library that you don't use.

This being said

  • If you're worried about your server workload : Use Google's CDN version.
  • If you're worried about browser's loading time : Don't. If you use jQuery on other pages, it will be cached by the browser.
  • If you're worried about browser's execution time : Don't. JQuery initialization is much too fast to notice.

Upvotes: 2

Curtis
Curtis

Reputation: 103358

That's correct, loading the jQuery only on pages which require it will help performance for the pages that don't use jQuery. However, if a page containing the jQuery library has already been viewed, this won't make much of a difference as the javascript file will have already been cached locally.

Generally, unless you are only using jQuery on 1% if your pages, I don't think it hurts to put jQuery in the head of every page.

If you are concerned about performance, you could link to the script hosted on Google. This has benefits because if another site the user has viewed has already downloaded this script, it will already be cached for your website.

https://developers.google.com/speed/libraries/devguide

Upvotes: 0

Related Questions