Saturday, 13 September 2014

Tips and Tricks to improve MVC Application Performance

In this post we will cover a few tips and tricks to improve ASP.NET MVC Application Performance:

(1) Run in Release mode

Make sure your production application always runs in release mode in the web.config
<compilation debug="false"></compilation>
Or change this in the machine.config on the production servers
          <deployment retail="true"></deployment>


(2) Only use the View Engines that you require

protected void Application_Start()
    ViewEngines.Engines.Add(new RazorViewEngine());


(3) Use the CachedDataAnnotationsModelMetadataProvider

ModelMetadataProviders.Current = new CachedDataAnnotationsModelMetadataProvider();

(4) Avoid passing null models to views

Because a NullReferenceException will be thrown when the expression gets evaluated, which .NET then has to handle gracefully.
// BAD
public ActionResult Profile()
    return View();

public ActionResult Profile()
    return View(new Profile());


(5) Use OutputCacheAttribute when appropriate

For content that does not change often, use the OutputCacheAttribute to save unnecessary and action executions.
<urlcompression dodynamiccompression="true" dostaticcompression="true" dynamiccompressionbeforecache="true"></urlcompression>



(6) Remove unused HTTP Modules

If you run into any problems after removing them, try adding them back in.
      <remove name="WindowsAuthentication"></remove>
      <remove name="PassportAuthentication"></remove>
      <remove name="Profile"></remove>
      <remove name="AnonymousIdentification"></remove>



(7) Flush your HTML as soon as it is generated

<pages buffer="true" enableviewstate="false"></pages>



(8) Turn off Tracing

          <trace enabled="false"></trace>



(9) Remove HTTP Headers

This is more of a security thing:
    <httpruntime enableversionheader="false"></httpruntime>

          <remove name="X-Powered-By"></remove>



(10) Uninstall the URL Rewrite module if not required

This saves CPU cycles used to check the server variable for each request.
Go to "Add or Remove Programs" and find "Microsoft URL Rewrite Module" and select uninstall. 

(11) Minify Js, CSS, Gzip Compress
Minification involves removing unnecessary characters from code to reduce its size. This reduction in size greatly improves load times. There are some great tools out there to use - JSMin, YUI Compressor, etc.

(12) Use a Content Delivery Network
Serving your content from multiple worldwide servers allows much faster loads time on the user's side. These servers can be used to cache and deliver all types of content. On this site I am using the Google CDN to load my jQuery API. To do this, you simply need to replace your local version of jQuery with the one on the CDN:
<script type="text/javascript" src=""></script>

Google has support for loads of different javascript libraries. Microsoft also offers Microsoft AJAX CDN, which service you choose to go with is all up to you.

Top JavaScript MVC Frameworks

As more and more logic ends up being executed in the browser, JavaScript front-end codebases grow larger and more difficult to maintain. As a way to solve this issue developers have been turning to MVC frameworks which promise increased productivity and maintainable code.
As part of the new community-driven research initiative, InfoQ is examining the adoption of such frameworks and libraries by developers.
1) Backbone.js: Provides models with key-value binding and custom events, collections, and connects it all to your existing API over a RESTful JSON interface.
2)   AngularJS: A toolset based on extending the HTML vocabulary for your application.
3)  Ember.js: Provides template written in the Handlebars templating language, views, controllers, models and a router.
4) Knockout: Aims to simplify JavaScript UIs by applying the Model-View-View Model (MVVM) pattern.
5) Agility.js: Aims to let developers write maintainable and reusable browser code without the verbose or infrastructural overhead found in other MVC libraries.
6)  CanJS: Focuses on striking a balance between size, ease of use, safety, speed and flexibility.
7)  Spine: A lightweight framework that strives to have the most friendly documentation for any JavaScript framework available.
8)  Maria: Based on the original MVC flavor as it was used in Smalltalk - aka "the Gang of Four MVC".
9)   ExtJS: Amongst other things offers plugin-free charting, and modern UI widgets.
10) Sammy.js: A small JavaScript framework developed to provide a basic structure for developing JavaScript applications.
11)  Stapes.js: A tiny framework that aims to be easy to fit in an existing codebase, and because of its size it's suitable for mobile development.
12)   Epitome: Epitome is a MVC* (MVP) framework for MooTools.
13)   soma.js: Tries helping developers to write loosely-coupled applications to increase scalability and maintainability.
14)   PlastronJS: MVC framework for Closure Library and Closure Compiler.
15) rAppid.js: Lets you encapsulate complexity into components which can be easy used like HTML elements in your application.
16)   Serenade.js: Tries to follow the ideas of classical MVC than competing frameworks.
17) Kendo UI: Combines jQuery-based widgets, an MVVM framework, themes, templates, and more.