Click here to Skip to main content
15,892,965 members
Articles / Web Development / HTML
Article

Performance Improvement For AJAX

Rate me:
Please Sign up or sign in to vote.
2.00/5 (12 votes)
10 May 20062 min read 36.6K   34   1
A simple tip to improve performance when using AJAX in WebForms.

Introduction

In Web Forms, we use AJAX to invoke server side methods from the client-side (from JavaScript). AJAX internally uses XMLHttpRequest. I have tested a number of AJAX functions implemented in different ways. Also, I have monitored the performance and life cycle of AJAX calls. I have found some serious problems while using AJAX in Web Forms. And, I found a solution for that problem. In this article, I am going to share the problem and the solution.

Performance problems while using AJAX

For each AJAX call, we create an instance of the class which contains the AJAX method. Also, it will create instances for fields, properties, and other class level variables, if we use the new keyword in the class level.

Proof of concept

I have created a project which contains two Web Forms, WebForm1.aspx, WebForm2.aspx, and a class, Student.vb. Both code-behind pages have a getData() AJAX function and a public variable of type Student. Using the MXLogger class, I have logged each stages of the execution flow.

Note: Webform2.aspx’s GetData() AJAX function is Shared. In WebForm1, it is not Shared.

VB
'Student.vb
Public Class Student
    
    Sub New()
        MXLogger.AddLog("From Student.Constructor")
    End Sub
    
    Dim _Name As String
   
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal Value As String)
            _Name = Value
        End Set
    End Property

End Class


'WebForm1.aspx.vb
Public Class WebForm1
    
    Public Student As New Student

    Sub New()
        MXLogger.AddLog("From WebForm1.Constructor")
    End Sub

    <Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)> _
    Public Function getData() As String
        MXLogger.AddLog("From WebForm1.Ajax.getData()")
        Return "I m a Non Shared Function"
    End Function
End Class


'WebForm2.aspx.vb
Public Class WebForm2
   
    Public Student As New Student

    Sub New()
        MXLogger.AddLog("From WebForm2.Constructor")
    End Sub

    <Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)> _
    Public Shared Function getData() As String
        MXLogger.AddLog("From WebForm2.Ajax.getData()")
        Return "I m a Shared Function"
    End Function
End Class

Testing the application

  • Test case 1:

    Run webform1.aspx and call the getData() AJAX function three times from the JavaScript.

  • Test case 2:

    Run webform2.aspx and call the getData() AJAX function three times from the JavaScript.

For the above test cases, I got logs as below:

//Please Note: Some of these log lines
// I have added Manually for explanation purpose.
 
LOG for the Test Case 1:  ( Non Ajax Shared Function )

-------While Loading The Page--------
5/9/2006 10:37:29 AM>>From Student.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Ajax.getData()
-------First Call For GetData()--------
5/9/2006 10:37:29 AM>>From Student.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Ajax.getData()
-------Second Call For GetData()--------
5/9/2006 10:37:29 AM>>From Student.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Constructor
5/9/2006 10:37:29 AM>>From WebForm1.Ajax.getData()
-------Third Call For GetData()--------
5/9/2006 10:37:30 AM>>From Student.Constructor
5/9/2006 10:37:30 AM>>From WebForm1.Constructor
5/9/2006 10:37:30 AM>>From WebForm1.Ajax.getData()

LOG for the Test Case 2:  ( Shared Ajax Function )

-------While Loading The Page--------
5/9/2006 10:37:09 AM>>From Student.Constructor
5/9/2006 10:37:09 AM>>From WebForm2.Constructor
5/9/2006 10:37:09 AM>>From WebForm2.Ajax.getData()
-------First Call For GetData()--------
5/9/2006 10:38:11 AM>>From WebForm2.Ajax.getData()
-------Second Call For GetData()--------
5/9/2006 10:38:11 AM>>From WebForm2.Ajax.getData()
-------Third Call For GetData()--------
5/9/2006 10:38:11 AM>>From WebForm2.Ajax.getData()

In the above logs, we can see that for Test Case 1, we can see more logs which are from the constructors of Webform1 and Student.

Conclusion

My suggestion is, in all possible places, we should use the Shared method for AJAX, so that it will not create more instances of the Web Form as well as the class level fields. Thus, we can reduce the number of Finalize() calls from the GC.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
"Manikandan Balakrishnan" Working as an IT consultant with LogicaCMG’s Global Service Delivery part-India, he has a good experience on Web/Win development (C#, Asp.net) and enterprise application integration (BizTalk) technologies. Prior to this he worked on world’s biggest e-learning product with Excel Soft Technologies, Mysore.

City: Coimbatore, TN
CreativeManix@gmail.com

Comments and Discussions

 
GeneralStrings attached Pin
gstolarov29-Apr-09 18:41
gstolarov29-Apr-09 18:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.