Wednesday 20 May 2009

Some tips to increase performance of your web site.






You're most likely familiar with the paradox - whether you're using brand-new technologies and have top hardware it looks your web is slow. Please find below some tips that can help you to improve web's performance.

1) Set debug="false" under compilation as follows:

<compilation language="c#" debug="false"></compilation>

2) Use Server.Transfer instead of Response.Redirect

3) Always check Page.IsValid when using Validator Controls

4) Use For Each loop instead of For loop for String Iteration.

5) Use Client-Side Validation. (but not all the time you have to validate even on the server side)

6) Check Page.IsPostBack to avoid repetition code execution.

7) Set trace enabled="false" unless until required. (by default it's off, use on the pages where it's required)

<trace enabled="false" requestlimit="10" pageoutput="false" tracemode="SortByTime" localonly="true"></trace>

8) Precompiled pages and disable AutoEventWireup; setting AutoEventWireup="false" in the Machine.config file.

9) If not required, set SessionState mode="Off".

<SessionState timeout="20" cookieless="false" mode="Off" StateConnectionString="tcpip=127.0.0.1:42424" SqlConnectionString="data source=127.0.0.1;Trusted_Connection=no"></SessionState>

10) Select the Release mode before making the final Build for your application. (by default, the mode is Debug)

11) Disable ViewState by setting EnableViewState="false" when not required.

12) Use Using as much as possible. This not very documented feature defines a scope, outside of which an object or objects will be disposed. VB.NET/C#, via the .NET Framework common language runtine, automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it's usually best to release limited resources such as file handles and network connections as quickly as possible. The Using statement allows the programmer to specify when objects that use resources should release them. The object provided to the Using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources. A Using statement can be exited either when the end of the Using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.

Dim sContent As String
Using sr As New IO.StreamReader("C:\test.txt")
sContent = sr.ReadToEnd
sr.Close()
End Using

13) Use Caching to improve the performance of your application.

i. Page output caching:
<%@ OutputCache duration="3600" varybyparam="none" %>

ii. Page fragment caching:
Write a Page output caching code into each User Control.

iii. Data caching:
Imports System.Data.SqlClient
Imports System.Data


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


Dim dv As DataView = Cache.Get("ProductDataView")

If dv Is Nothing Then
Using conn As New SqlConnection("Data Source=LAPTOP;Initial Catalog=Customers;Integrated Security=True")


Using da As New SqlDataAdapter("Select ProductId, ProductName From dbo.Product (noLock)", conn)

Using ds As New DataSet
da.Fill(ds, "Product")
dv = ds.Tables("Product").DefaultView
Cache.Insert("ProductDataView", dv)
End Using
End Using
End Using
Else

Response.Write("<h2>Loaded Data From Cache</h2>")
GridView1.DataSource = dv
GridView1.DataBind()
End If
End Sub

End Class

14) Use Finally method to kill resources. (but not in the case of Using)

15) The String and StringBuilder magic.

It's nice to use StringBuilder instead of String when string are Amended. Strings occupy different memory location in every time of amended where StringBuilder use single memory location.

Dim vars As New StringBuilder
For Each sv As String In Request.ServerVariables
vars.Append(sv & "<br />")
Next
Response.Write(vars.ToString.ToLower)

16) Use strString = String.Empty instead of strString = ""

17) Never use object value directly; first get object value in local variable and then use. It takes more time than variable reading.

18) Don't make the member variables Public or Protected, try to keep Private and use public/protected as properties.

19) Avoid Exceptions: Use If condition (if it is check proper condition).

20) Code Optimalization: Avoid using code like x = x + 1; it's better to use x += 1.

21) Data Access Techniques: DataReaders provide a fast and efficient method of data retrieval. DataReader is much faster than DataSets as far as performance is concerned.

22) Before doing a bulky ASP code processing, you can check to make sure Response.IsClientConnected.

23) Avoid Session variables because each ASP page runs in a different thread and session calls will be serialized one by one. So, this will slow down the application. Instead of Session variables you can use the QueryString collection or hidden variables in the form which holds the values.

24) To improve performance, set Response.Buffer = True.

<% Response.Buffer = True %>

Then use:

<% Response.Flush = True %>

25) Avoid frequent round trips to the Database.

26) Use Repeater control instead of DataGrid, DataList because it's efficient, customizable, and programmable.

27) Data listing is more time consume when large data are retrieve from Database.

Paging will display only particular data but take load of all data.

Fetch only data that is needed for current page.

28) Reduce Cookie size.

29) Avoid Inline JavaScript and CSS.

30) Use single CSS file instead of multiple CSS files.

Lot of .CSS files will cause a large amount of requests, regardless of the file sizes.

.CSS files are normally cached by browsers, so a single and heavy .CSS file doesn't cause a long wait on each page request.

Inline .CSS classes could make HTML heavy.

31) Compress CSS, JavaScript, Images and try use server side compression.

CSS compression tool

JavaScript compression tool

Image compression: GIF and PNG are similar, but PNG typically produces a lower file size (Note: some browser not supporting PNG format)

Server side compression tool

No comments: