views:

550

answers:

5

I'm trying to understand how CSS sprites improve performance on a site?

Why is the downloading of several small images slower than the download of a single image holding the smaller images if the total size of the single image is the sum of the smaller images?

+15  A: 

Fewer round-trips to the server. Instead of 6 (say) requests for 6 different images, you get one request and 6 uses of the same image. If the server is going to respond "it hasn't changed since the last time you asked" most of the time, that can be a significant reduction in the amount of network traffic.

Paul Tomblin
I did some numbers a few years ago - for one 43 byte 1x1 gif, the HTTP overhead (not includinany g TCP packet overhead) was in excess of 240 bytes.
Alister Bulman
You could add, that depending on the image, the compression can be more efficient.
Georg
Also, TCP/IP is more efficient when dealing when transferring larger files.
David
Also, if you have the background-image style only once in your CSS (and apply the same class everywhere), there is going to be only 1 request for the image. (Not multiple 304s)
Chetan Sastry
+14  A: 

Because multiple images require multiple http requests. See Yahoo's performance rule #1: Minimize HTTP Requests.

Michael Myers
+3  A: 

In addition to minimizing the number of requests, depending on the images, you also might find that the file size is smaller combined than it would be if they are separated (due, I think, to the reduced amount of metadata, among other things). Another added benefit to using sprites is that you don't have the flicker effect when you first hover over an element that has a hover state, which can improve user perception of your page's performance. An interesting resource on image optimization you might want to read is this series of blog posts on the Yahoo User Interface Blog. On rereading Yahoo's recommended practices for performance, I was surprised to see that they also suggested that arranging your images horizontally rather than vertically can also reduce your file size.

VirtuosiMedia
+19  A: 

It's important to understand why the overhead of an HTTP request has such an impact.

In its simplest form, an HTTP request consists of opening a socket, sending the request on the open socket and reading the response.

To open a socket, the client's TCP/IP stack sends a TCP SYN packet to the server. The server responds with a SYN-ACK, and the client responds to that with an ACK.

So, before you send a single byte of application data, you have to wait for a whole one and a half round trips to the server, at least.

Then the client needs to send the request, wait for the server to parse the request, find the requested data, send it back - that's another round trip plus some server side overhead (hopefully a small overhead, although I've seen some slow servers) plus the time to transmit the actual data, and that's the best case, assuming no network congestion which would result in packets being dropped and retransmitted.

Every chance you have to avoid this, you should.

Modern browsers will issue multiple requests in parallel in an attempt to reduce some of the overhead involved. HTTP requests can theoretically be done on the same socket, making things a little better. But in general, network round trips are bad for performance, and should be avoided.

Ori Pessach
+1  A: 

Aside from the reasons above, I find them easier to work with. You only have one file you need to modify and upload, and one URL to change in your code, if you update the image.

Mark
This is actually not the case - sprited images are much harder to deal with, since you have to keep track of the subimage coordinates in the main image.
levik
Remember that "above" can change: if your answer is voted up, or others come in after you, you could end up being above the others. Use the user's username or usernames when referring to other comments here and it'll make sense no matter what.
Paul Kroll