Search code examples
xmlhttprequesthttp2

With http2, does number of XHRs have any effect on performance if overall data size is the same?


As far as I know, HTTP/2 no longer uses separate TCP connections for every request, which is the main performance-booster of the protocol.

Does that mean it doesn't matter whether I use 10 XHRs with 10kB of content each or one XHR with 100kB and then split the parts client-side?


Solution

  • A precise answer would require a benchmark for your specific case.

    In more general terms, from the client point of view, if you can make the 10 XHR at the same time (for example, in a tight loop), then what happens is that those 10 requests will leave the client more or less at the same time, incur in the latency between the client and the server, be processed on the server (more or less in parallel depending on the server architecture), so the result could be similar of a single XHR - although I would expect the single request to be more efficient.

    From the server point of view, however, things may be different. If you multiply by 10 what could have been done with a single request, now your server sees a 10x increase in request rate. Reading from the network, request parsing and request dispatching are all activities that are heavily optimized in servers, but they do have a cost, and a 10x increase in that cost may be noticeable.

    And that 10x increase in request rate to the server may impact the database as well, the filesystem, etc. so there may be ripple effects that can only be noticed by actually performing the benchmark.

    Other things that you have to weigh are the amount of work that you need to do in the server to aggregate things, and to split them on the client; along with other less measurable things like code clarity and maintainability, and so forth.

    I would say that common pragmatic judgement applies here: if you can make the same work with one request, why making 10 requests ? Do you have a more specific example ?

    If you are in doubt, measure.