HTTP Requests vs File Size?

后端 未结 2 466
暗喜
暗喜 2020-12-29 06:37

At what point is better to have more HTTP Requests if that means the page size goes down? For example, if I have an image that is 20KB, how much size would I have to reduce

相关标签:
2条回答
  • 2020-12-29 07:08

    The conclusion of josh's answer didn't really changed. Considering the "Mobile Network Experience Report Jan. 2020" the latency has decreased by around 30% (from 80ms to approx. 55ms) but the average download rate (mobile) has increased up to 23 MB/s for the lowest rated operator.

    So, we came up with this theoretical numbers for the lowest rated US mobile operater of 2019:

    1x 20 kB files:  55ms latency + 7ms transfer time = 62ms
    2x 4 kB files:  110ms latency + 2 ms transfer time = 112ms
    
    0 讨论(0)
  • 2020-12-29 07:24

    The practical answer is never, especially when you're talking about relatively minuscule amounts of data like a kilobyte or two.

    The real enemy of a web page's performance is not the number of bytes transferred; rather, it is network latency. Let's take your example and consider a 5 Mb/s connection (the average connection speed in the US is a little over that) with a ping time to your server of 80ms:

    1x 20 kB files:  80ms latency + 31ms transfer time = 111ms
    2x 4 kB files:  160ms latency + 13ms transfer time = 173ms
    

    This "optimization" just cost at least 62ms with all other variables being equal. In the real world, I'd wager that performance would be even worse due to things like extra server load.

    Also consider that you're now using an extra one of the limited number of parallel requests a browser will make (somewhere between 2 and 8 depending on browser) for a half of an image rather than something more valuable like script, CSS, or other non-spritable image. This will slow down the overall load time of your page.

    Furthermore, I have a suspicion that your entire premise is flawed. In general, splitting an image into two files cannot truly yield a smaller overall file size because every image container format has header data; for example, a PNG file has at least 57 bytes of overhead before any actual image data. Plus, an extra HTTP request means an additional ± 800-900 bytes of overhead over the wire.

    I suspect you'll find that one properly compressed PNG will be no larger than the total size of two PNGs making up the same image.


    (source: josh3736.net)
    (1027 bytes)


    (source: josh3736.net)

    (source: josh3736.net)
    (730 + 809 = 1539 bytes)

    Even though the first single PNG has 150x100 pixels of "dead" transparent space, it is 33% smaller than the two PNGs that represent the same image. (Disregard that I can't align the <img> tags properly here to make the two examples look the same.)

    0 讨论(0)
提交回复
热议问题