Johan
Johan

Reputation: 35194

Improve responsetime from generic handler made by a jquery ajax call?

I have made a generic handler (.ashx) to fetch some database values using jquery ajax. I have a few questions:

  1. Why is the first call to the handler always the slowest one?

  2. Is there any way to decrease the response-time from the point that the HTTP-get/post is made until i get back a response from the handler? My current code looks something like this:

    public void ProcessRequest(HttpContext context)
    {
         HttpContext.Current.Response.ContentType = "application/json";
         SOFAEntities ctx = new SOFAEntities();
         JavaScriptSerializer serializer = new JavaScriptSerializer();
         string systemKey = HttpContext.Current.Request["SystemKey"];
    
         try
        {
            SYSTEM_AUDIT_SHEET auditSheet = ctx.SYSTEM_AUDIT_SHEET.Where(s => s.SYSTEM_KEY == systemKeyDec).Select(s => s).OrderByDescending(s => s.AUDIT_SHEET_VERSION).First();
            HttpContext.Current.Response.Write(serializer.Serialize(auditSheet));
        }
        catch (Exception e)
        {
            HttpContext.Current.Response.Write(serializer.Serialize(new ErrorObj() { ErrorMessage = e.Message }));
        }
    }
    

Upvotes: 1

Views: 441

Answers (1)

Guffa
Guffa

Reputation: 700342

1: There are several possible reasons, which all can add up to make the first request slower, like:

  • The code has to be compiled / JITted
  • There is no database connection in the pool, so one has to be established
  • The database request is not cached in the datanase

2: You could serialise the objects manually, instead of using the generic serialiser, which uses reflection.

Upvotes: 1

Related Questions