Click here to Skip to main content
6,295,667 members and growing! (12,000 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Intermediate

Performance Improvement For AJAX

By Manikandan.net

A simple tip to improve performance when using AJAX in WebForms.
VB, Javascript, XML, HTML, Windows, .NET, ASP.NET, Visual Studio, Ajax, Dev
Posted:10 May 2006
Views:20,072
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
11 votes for this article.
Popularity: 2.43 Rating: 2.33 out of 5
4 votes, 36.4%
1
2 votes, 18.2%
2
2 votes, 18.2%
3

4
3 votes, 27.3%
5

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.

'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

About the Author

Manikandan.net


Member
"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

Occupation: Web Developer
Location: India India

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralStrings attached Pinmembergstolarov19:41 29 Apr '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 May 2006
Editor: Smitha Vijayan
Copyright 2006 by Manikandan.net
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project