homepage Welcome to WebmasterWorld Guest from 54.211.95.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Remote Scripts - best practice for speed
Is it better to link to a remote script, or embed it directly on your site?
MadDogMike




msg:4631677
 1:36 am on Dec 18, 2013 (gmt 0)

So, I use a script like this on my website:

<script type="text/javascript" src="http://scriptsource.com/whatever-min.js"></script>

From a performance/speed perspective, is it better to pull a script from a remote source like that, or embed the whole script directly on my site?

 

phranque




msg:4631763
 8:54 am on Dec 18, 2013 (gmt 0)

it's a tradeoff.
if you put a script in your document it saves an extra request for a server resource.
however an external script can be cached by the browser and reused for later visits or on other pages.
an external script also reduces the size of the html document.
in some cases, serving an external script from a separate domain, especially if it's on a content delivery network, can help performance.
if the script is served from a cookieless domain that can also improve performance.

lucy24




msg:4631765
 9:10 am on Dec 18, 2013 (gmt 0)

in some cases, serving an external script from a separate domain, especially if it's on a content delivery network, can help performance.

Can the connection be so much faster that it outweighs the extra time involved in looking up a whole other domain?

DrDoc




msg:4631851
 4:06 pm on Dec 18, 2013 (gmt 0)

No, first connections anywhere are always made at a loss in performance. But, assuming you are not using some fringe library, chances are that people have common domains cached in their browser/router/DNS.

phranque




msg:4631852
 4:09 pm on Dec 18, 2013 (gmt 0)

your browser limits the number of simultaneous connections to a server, so the extra DNS lookup is yet another tradeoff vs the total number of connections required (aka "in some cases")

the CDN tradeoff is more DNS lookups vs lower latency and perhaps more cached resources.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved