Search code examples
performancehttprelative-pathabsolute-pathhotlinking

Performance: Absolute vs. Relative URLs


What's faster? Hot linking (inline linking) to an absolute URI or hosting the resource yourself and using a relative URI?

In his tutorial on how to style HTML5 elements in Internet Explorer, Remy Sharp states that hot linking causes an "extra HTTP [GET] request." I agree if you're comparing hot linking to copying & pasting (embedding) the script into the HTML. But, if you're comparing hot linking to hosting the script locally and linking via a relative path, then I'd argue that hot linking is actually (ever-so-slightly) faster because the browser doesn't have to resolve the absolute URL from the relative path. In both cases, however, an extra HTTP GET request is performed, correct?


Solution

  • The correct answer is - it depends.

    Hotlinking can be slow because -

    1. An extra DNS lookup is needed
    2. Unable to reuse an existing TCP/IP socket connection

    Hosting on your server can be slow because -

    1. Browsers only allow n concurrent requests per host. Having one more request to the same host has the potential to introduce queuing, which can be slow. The number 'n' is browser specific, and is anywhere between 2 and 6. See browserscope > network > connections per host name.

    If you assume both servers are identical in every respect, I'd say hosting on your server is going to be faster. This is true especially on new browsers where the number of connections per host is 6.

    But sadly, things are never so simple. I'd recommend using hotlinking only if -

    1. You have too many resources (images/js) on your domain
    2. The other server is a CDN, and the resource is a popular enough so that there is a decent chance it will be present in the browser's cache. Think JQuery on google's servers.

    For all other use cases, you are better off hosting on your own servers.