Tips And Tricks: Performance Tips and Techniques in .Net

Few Performance Tips in .Net

(will be updated regularly)

Following are few tips collated from different reading material found in Books and Internet.

1) Shortcut assignment operators

Using the shortcut assignment operators reduces the typing you need to do. They are also more efficient at runtime, because the variable needs to be evaluated only once.

commonly used shortcut assignment operators like these +=, -=, *=, /=, \=, &=,should be used.

For Eg. Use sum += value instead of sum= sum+ value. Since, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of sum, and many times this is not needed. The first statement can be optimized far better than the second, since the JIT can avoid evaluating the sum twice.

2) Exception Handling

The general rule of thumb is to put exception as close to the end user-generated event that caused the issue as possible, but still retain enough information to handle the exception as needed. For example, the structured error handler would be in the Click event procedure for the button(Submit/Save).

Another rule of thumb is to never catch an exception unless you plan to do something with it.You should not catch an exception and then simply rethrow it. That has a negative impact on performance and provides no benefit.

Throwing exceptions can be very expensive, so make sure to minimize this cost.You can use Perfmon to check the number of exceptions your application is throwing.

The run time can throw exceptions on its own! For example, Response.Redirect() throws a ThreadAbort exception. Even if you don’t explicitly throw exceptions, you may use functions that do. Make sure you check Perfmon to get the real story, and the debugger to check the source.

Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. Make sure that never use exceptions to control the application flow,instead check for the condition which causes the exception.

3) Keep DOM Updates to a Minimum

To do anything interesting, your code has to modify the DOM. However keep in mind that changes to the DOM are extremely expensive. Understandably, the browser has to do quite a bit of work to handle calls to the DOM API that change the content of the page. Even more surprisingly, calls to methods that do not change the page can be particularly slow, as, for example, registering listeners.
Also beware of code that iterates over a large number of DOM objects, such as code that iterates over document.all to look up a particular element, or uses document.getElementByName(). When using
those, as the size of your page grows so will the time taken by your code to execute.

4) Handling Memory Leaks

Event listeners are a major cause of memory leaks, both on Internet Explorer and Firefox, because event listeners routinely refer to other elements. The way to get around this problem is to keep track of every event listener you register and to unregister all those listeners when the user navigates away from the page, thus breaking the loop and allowing the garbage collector to clean up afterwards.

On Firefox, the Leak Monitor extension will help you detect memory leaks in your pages. When installed, as you navigate away from a page, the extension will display a pop-up with the list of leaked objects for that page. If no pop-up is displayed, it means that no memory leak was caused by that page.
You can download and install the Leak Monitor extension from

5) Cache Aggressively

There are several features and tools in ASP that you can make use of to gain performance.
Output Caching—Stores the static result of an ASP request. Specified using the directive:

Duration—Time item exists in the cache
VaryByParam—Varies cache entries by Get/Post params
VaryByHeader—Varies cache entries by Http header
VaryByCustom—Varies cache entries by browser
Override to vary by whatever you want:
Fragment Caching—When it is not possible to store an entire page (privacy, personalization, dynamic content), you can use fragment caching to store parts of it for quicker retrieval later.
a) VaryByControl—Varies the cached items by values of a control

Cache API—Provides extremely fine granularity for caching by keeping a hashtable of cached objects in memory (System.web.UI.caching). It also:
a) Includes Dependencies (key, file, time)

b) Automatically expires unused items

c) Supports Callbacks

Caching intelligently can give you excellent performance, and it’s important to think about what kind of caching you need.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: