6

A coworker just asked me if there were any reason why referring to images with a relative path would impede site speed.

While for cleanliness it's a good idea to have the fewer characters of a relative path, but wondering if there are other slowdowns/consequences to using absolute/full paths? I'm thinking there may be a DNS lookup involved by having the full path.

What are the other consequences, if any?

Smamatti
  • 3,901
  • 3
  • 32
  • 43
Keefer
  • 2,269
  • 7
  • 33
  • 50

3 Answers3

7

Using absolute paths forces the web server to establish a connection, send and receive the HTTP requests. If using relative, the connection is already established, so it doesn't have to go through that logic (hence increasing page load speed). You probably won't see an amazing difference, but every bit saved is a good thing, right?


Edit: After doing a quick test, the difference is extremely negligible, and doesn't seem to produce that great of a case for my answer. I created a test page with the same image twice, one with relative and one with absolute path: http://damonbauer.me/test/index.html.

Test One: Image w/ Absolute path in HTML code first: (click for larger version) http://damonbauer.me/test/images/results1.jpg

The absolute path image took 869ms to load, while the relative path image, listed second in the HTML code, loaded in 635ms.

Test Two: Image w/ Relative path in HTML code first: (click for larger version) http://damonbauer.me/test/images/results1.jpg

The absolute path image took 303ms to load, while the relative path image, listed first in the HTML code, loaded in 315ms.

My opinion? It's faster to load using relative. Even when listed after the absolute path image, it took only 12ms longer for the relative image to load. When the absolute path image was loaded second, it took it 234ms longer to load. In both cases, they are close, and it looks to me like it matters more about what loads first. Either way, I would go with relative, if only for portability's sake.

Damon Bauer
  • 2,718
  • 1
  • 22
  • 35
  • 2
    Are you sure that no client is implemented smart and checks the base URI against the current page? I'd love to see some sniffing on network traffic or similar stuff. -- Sorry, but sounds more like a thesis to me atm. – Smamatti Dec 01 '11 at 19:18
  • @Smamatti - added example. It's low level and the results aren't mind blowing, but it does the job. – Damon Bauer Dec 01 '11 at 19:46
  • Not really as detailed as I wish it would be, but thanks for trying and posting. -- I tried it myself and this thesis seems to be wrong. Sorry. -- My _benchmark_ http://img560.imageshack.us/img560/4429/imgpathtypes.jpg (Yes I cleared the cache etc.) – Smamatti Dec 01 '11 at 20:16
  • Could be any number of things. I cleared my cache before each test too. I'm not saying I'm right or wrong, just posting my findings at this point. – Damon Bauer Dec 01 '11 at 20:35
  • 1
    I think this is BS. The browser logic to re-use a connection should not be preempted by using absolute paths. BTW - there are two kinds of absolute paths. Those that begin with 'http://' and those that begin with '/' (or '//'). – mckoss Mar 29 '13 at 07:23
  • totally nonsense! what make you think loading absolute path is slower?the testing you done is incorrect, I bet if you swap the test cases, you get better performance for absolute path – ajreal Apr 25 '13 at 01:43
3

nah, there isn't any noticeable difference, and both have their uses. There is no DNS lookup client-side, it's the browser (or maybe the web server?) that changes the url to what it should be. Use them as what you need to, relative paths are more portable (no need to do anything to make them work in your development or live server), while absolute paths take you to specific location (regardless in what server you are).
In my case, I use relative paths unless I want a specific address to be used. Also, when you're switching from non-secure to secure, you'd want to specify https so full path (or do an extra redirect somewhere else)

Rodolfo
  • 4,155
  • 23
  • 38
3

A remote absolute path will go thru DNS but it frees up your web server to serve pages while another server gets the work of serving images. That lightens the network load on the page server and speeds things up.

A local absolute path will be the same as local relative in that after the first page it, it will be cached by the web server and it's not going to matter after that.

MrG
  • 1,525
  • 13
  • 24