Sunday, May 27, 2012

How much more efficient is one big image rather than many small images. Facebook style


So I was looking at the facebook HTML with firebug, and I chanced upon this image and came to the conclusion that facebook uses this large image (with tricky image positioning code) rather than many small ones for its graphical elements. Is this more efficient than storing many small images?



Can anybody give any clues as to why facebook would do this.


Source: Tips4all

11 comments:

  1. These are called CSS sprites, and yes, they're more efficient - the user only has to download one file, which reduces the number of HTTP requests to load the page. See this page for more info.

    ReplyDelete
  2. The theory behind that is explained here: http://css-tricks.com/css-sprites/, also have a look here, as it underlines why do you really need one big image.
    I also say that all the article is interesting :)

    ReplyDelete
  3. The problem with the pro-performance viewpoint is that it always seems to present the "Why" (performance), often without the "How", and never "Why Not".

    CSS Sprites do have a positive impact on performance, for reasons that other posters here have gone into in detail. However, they do have a downside: maintainability; removing, changing, and particularly resizing images becomes more difficult - mostly because of the alterations that need to be made to the background-position-riddled stylesheet along with every change to the size of a sprite, or to the shape of the map.

    I think it's a minority view, but I firmly believe that maintainability concerns should outweigh performance concerns in the vast majority of cases. If the performance is necessary, then go ahead, but be aware of the cost.

    That said, the performance impact is massive - particularly when you're using rollovers and want to avoid that effect you get when you mouseover an image then the browser goes away to request the rollover. It's appropriate to refactor your images into a sprite map once your requirements have settled down - particularly if your site is going to be under heavy traffic (and certainly the big examples people have been pulling out - facebook, amazon, yahoo - all fit that profile).

    There are a few cases where you can use them with basically no cost. Any time you're slicing an image, using a single image and background-position tags is probably cheaper. Any time you've got a standard set of icons - especially if they're of uniform size and unlikely to change. Plus, of course, any time when the performance really matters, and you've got the budget to cover the maintenance.

    If at all possible, use a tool and document your use of it so that whoever has to maintain your sprites knows about it. http://csssprites.org/ is the only tool I've looked into in any detail, but http://spriteme.org/ looks seriously awesome.

    ReplyDelete
  4. The technique is dubbed "css sprites".

    See:


    What are the advantages of using CSS
    Sprites in web applications?
    Performance of css sprites
    How do CSS sprites speed up a web
    site?

    ReplyDelete
  5. Since other users have answered this already, here's how to do it, and another way is here.

    ReplyDelete
  6. Opening connections is more costly than simply continuing a transfer. Similarly, the browser only needs to cache one file instead of hundreds.

    ReplyDelete
  7. yet another resource: http://www.smashingmagazine.com/2009/04/27/the-mystery-of-css-sprites-techniques-tools-and-tutorials/

    ReplyDelete
  8. One of the major benefits of CSS sprites is that it add virtually 0 server overhead and is all calculated client side. A huge gain for no server side performance hit.

    ReplyDelete
  9. Simple answer, you only have to 'fetch' one image file and it is 'cut' for different views, if you used multiple images that would be multiple files you would need to download, which simply would equate into additional time to download everything.

    Cutting up the large image into 'sprites' makes one HTTP request and provides a no flicker approach as well to 'onmouseover' elements (if you reuse the same large image for a mouse over effect).

    ReplyDelete
  10. Css Sprites tecnique is a method for reducing the number of image requests using background position.
    Best Practices for Speeding Up Your Web Site

    CSS Sprites: Image Slicing’s Kiss of Death

    ReplyDelete
  11. Google also does it - I've written a blog post on it here: http://www.stevefenton.co.uk/Content/Blog/Date/200905/Blog/Google-Uses-Image-Sprites/

    But the essence of it is that you make a single http request for one big image, rather than 20 small http requests.

    If you watch a http request, they spend more time waiting to start downloading than actually downloading, so it's much faster to do it in one hit - chunky, not chatty!

    ReplyDelete