Welcome to WebmasterWorld Guest from 54.166.197.230

Forum Moderators: open

Message Too Old, No Replies

Remote Scripts - best practice for speed

Is it better to link to a remote script, or embed it directly on your site?

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

New User

joined:July 4, 2013
posts: 8
votes: 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?
8:54 am on Dec 18, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10847
votes: 61


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.
9:10 am on Dec 18, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13676
votes: 440


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?
4:06 pm on Dec 18, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 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.
4:09 pm on Dec 18, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10847
votes: 61


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.