Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce/eliminate wait time/latency in AJAX request

I have a ASP.NET MVC3 project where the main aspx page is doing a dynamic loading for its parts using jQuery ajax. So basically when the site loads, it hits /Home/Index and then within Index (aspx) view, there are several lines of jQuery that make ajax calls (to /Home/PartOne and /Home/PartTwo to populate parts of the page.

So every time the page loads, it basically is doing 3 requests: to get index, to get PartOne, and then PartTwo.

The question: Why is there some kind of "wait" time for the the third request to execute? I thought this was the browser concurrent request limit, but why isn't it executing after the 1st request is done?

When I experimentally put "OutputCache" attribute on "PartTwo", it behaves as expected, that it was executing fast. This hints that the problem is not in IIS, but somewhere after that and before it hits my action method.

Here is a screen shot from Chrome network profiler: Chrome network profiler

Here is a screen shot on the MvcMiniProfiler - look at the 3rd row/value, it is waiting for 500ms before executing my controller's action code. enter image description here

My controller code looks like this. Although I snipped the actual code, but code for PartTwo is very trivial (no long computation, no db calls, etc):

public class HomeController : Controller {
    public ActionResult Index() {
        // do something here
        return View();
    }

    public ActionResult PartOne() {
        // do something here
        return View();
    }

    public ActionResult PartTwo() {
        // do something here
        return View();
    }
}

My javascript:

$(document).ready(function () {
    $.ajax({
        url: "/Home/PartOne",
        cache: false,
        success: function (data, textStatus, request) {
            $("#TestContainerOne").html(data);
        }
    });

    $.ajax({
        url: "/Home/PartTwo",
        cache: false,
        success: function (data, textStatus, request) {
            $("#TestContainerTwo").html(data);
        }
    });
});

My index.aspx:

<h2>Home page</h2>    
<div id="TestContainerOne"></div>
<div id="TestContainerTwo"></div>

PartOne.ascx:

<h2>Part One</h2>

PartTwo.ascx:

<h2>Part Two</h2>

Help?

like image 346
Johannes Setiabudi Avatar asked Mar 08 '12 21:03

Johannes Setiabudi


People also ask

How can I improve my Ajax performance?

Reducing the number of requests performed, and the amount of data included in both sides of the transaction, will have a dramatic impact on your Ajax performance. Making server changes will help, too, but may be beyond your control.

Why Ajax response is slow?

There are two different types of issues that can cause admin-ajax. php slow server responses. The first is a backend CPU issue and the second is more of a frontend issue where you will notice third party plugins polling this file in your website speed tests.

When working with Ajax which is faster?

They are all equally fast, the only question is which you find most readable. If you will be making numerous similar ajax calls then it is best to use $. ajaxSetup() in an accessible place (read: near top). This allows you to pre-set your most common settings, thereby creating your own short-cut function.

Does Ajax success wait for response?

Ajax requests do just that. With this, all codes following this one will execute immediately; you will not have to wait until the function returns. This way, your user would not have to wait for findItem() to return a result. Of course, because you still need an answer, you would have to include a callback.


1 Answers

You should use read only session state or disable it completely in order to get parallel processing of Ajax requests. By default the server locks the session state for requests coming from the same client, so the requests are executed sequential.

This is done by decorating your controller with the SessionState attribute:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class HomeController : Controller {
    public ActionResult Index() {
        // do something here
        return View();
    }

    public ActionResult PartOne() {
        // do something here
        return View();
    }

    public ActionResult PartTwo() {
        // do something here
        return View();
    }
}
like image 61
shizik Avatar answered Sep 29 '22 20:09

shizik