In my previous post, I talked about rendering partial views asynchronously. In this post, I will talk about the benefits of this approach by adding more performance improvement measures.
Output caching
As you might recall from my previous post, I showed an example of a controller action that renders a partial view. This action then was called by some jQuery code, which injected the HTML that was returned from the action. This approach also allows for granular output caching. Instead of caching an entire page, what you really want is to only cache certains sections of your page. This article explains all you need to know about output caching. In my example, we have the following situation:
The Index action just renders some HTML without any business logic. However, the GenerateSlideDeck method does contain business logic (i.e. data from the database). In my case, the data that is returned is fairly static data, which means caching is appropriate. Implementing Output Caching is very easy: just add an attribute above the action and you’re all set. In this example, I decided to set the expiration date to 4 hours:
For business logic that is not so static, output caching is probably not the best method. Don’t worry though, there are plenty of other tips and tricks.
Run in Release mode
This is a simple trick that should not be forgotten: deploy your application in release mode, not debug mode.
Bundling, minification and CDN
Bundling and minification come right out of the box in MVC applications. As soon as you create a new MVC5 application, some JavaScript libraries are already added to the project by default. Assess whether you need these and don’t forget to check if the library is already present before adding new files. Also apply this mechanism for custom scripts and stylesheets, it will keep your code clean and you will improve your site’s performance. A second consideration you should make is the use of content delivery networks. For the popular JavaScript libraries (such as jQuery), there are plenty of CDNs (Google being the most popular) that you can use. It is most likely that your visitors have visited other sites that also use the CDN that you use, so the files you are referring to are already cached in the visitor’s browser so there is no need to download these files again.
Await/Async
This is a relatively new feature that was released in .NET 4.5, but there is a lot of documentation available. When I have to create an application that requires data storage (in other words, every time), I leverage the repository pattern. I believe it is the best implementation of a data access layer. This approach makes it easy to implement asynchronous operations. In my synchronous repository class for Entity Framework, I have the following method:
This is the asynchronous way:
The conversion from synchronous to asynchronous comes down to the following steps:
- Wrap the return type type with the Action type
- Add ‘async’ to the method header
- Call the ToListAsync() method instead of ToList()
- Add the await keyword before calling the ToListAsync() method.
These steps also must be applied for all other methods that call this asynchronous method. An example is already visible above.
Conclusion
Many performance improving measure are also good for the architecture of your application. Some of the tips and tricks are easy to implement while others require more attention and coding. However, performance is a very important but underestimated part in software development. For more tips and tricks, please refer to this site