Combining Multiple Ajax Calls into One Call ASP.NET

Network roundtrip is the most expensive part of Ajax applications.You need to do everything you can to reduce the number of calls made to the server.Because each call has the overhead of connecting to the server and then downloading the response,each call wastes some time on the network.

When you have five calls going to theserver within a short time for some particular operation, you are wasting about one second (assuming 200 ms network latency) on the network.So,a popular practice among Ajax developers is to batch multiple consecutive single calls into one large call.This saves the network roundtrip as there’s only one network roundtrip to do, and thus the whole operation executes faster than making individual calls.

For example, say a user clicks on five different widgets from the widget gallery to add those widgets on the page.If the user is clicking quickly, instead of making one web service call per click and showing the newly added widget on the browser,the batcc feature waits until the user has clicked several times and then makes a batch call to the server, sending all the widget names at once.

All the widgets are downloaded and created in one shot.This saves a network roundtrip and condenses multiple single calls to the web service.Although the total time for a batch call will always be lessthan each individual call,it does not always make the user experience faster.The actual response time might be reduced, but the perceived delay is higher because the user will see nothing happen until multiple widgets are downloaded and created on the browser.

If three web service calls are batched into one call, the first single call doesn’t finish first —all three calls finish at the same time. If you are doing some UI updates that wait for the completion of each web service call, all of the calls complete in one shot and then the UI is updated. As a result, you do not see incremental up dates on the UI —instead, a long delay before the UI updates.

If any of the calls download a lot of data, the user will see nothing happen until all three calls complete. So, the duration of the first call becomes nearly the duration of the sum of all three calls. Although the total duration is reduced, the perceived duration is higher. Batch calls are handy when each call is transmitting a small amount of data because the calls are executed in one roundtrip and the perceived speed is good.

Let’s work on a scenario where three calls are made one at a time. Figure 6-1 shows how the calls are executed.

The browser makes two calls at a time, so the third call needs to wait until one of the earlier calls completes

The browser makes two calls at a time, so the third call needs to wait until one of the earlier calls completes

The second call takes a bit more time to reach the server because the first call is eating up the bandwidth.For the same reason,it takes longer to download.Figure shows these three calls batched into one.

The batch call function combines multiple unit calls and executes them in one shot,which means there’s only one request to server and one response to download

batch call function combines multiple unit calls

The total download time is also reduced if the IIS response compression is enabled and if there is only one network latency.All three calls are executed on the server in one request, and the combined response is downloaded to one call.The total duration to complete the whole batch is always less than that for two calls.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

ASP.NET Topics