14 Tips to Improve the Performance of the ASP.NET Core Web Application (Part 1)
Performance is the major factor for the success of any web application development. ASP.NET Core is one of the best platforms for high-performance web application development. In this article, I am going to share 14 tips that can help your web application perform better. Let's explore them:
Caching is a last resort
Projects that use multiple levels of cache often
demonstrate a misunderstanding of why caching is
required in the first place. Caching is not synonymous
with performance. Your code should already be efficient.
Caching should only be used as a last resort, after
you’ve made all possible (and sensible) code
optimizations.
Enable HTTP compression
Make sure HTTP compression is turned on for any
uncompressed content. HTML in particular compresses
significantly, and in this era of mobile friendliness
and slow 3G connections, that’s essential.
ORM database profiling
Always profile your ORM database hits with SQL Profiler
during development. ORMs get away from you very quickly.
Before you know it, you’ve run a query 2000 times in a
loop, when you could have retrieved all your data with a
single database hit.
Lazy loading best practice
Watch out for lazy loading in ORMs. You shouldn’t lazy
load any entities that could be retrieved with a single
database hit.
Make sure paging is conducted at the database layer
You should carefully consider how paging is
implemented. Many controls implement paging in a
simplistic fashion, where the database is required to
return all available data and the control limits what is
shown. This strategy is fraught with performance
problems, as it means that all the data in the given set
must be extracted from the database (e.g. all customers
or orders). Depending on the records involved, this
could cause significant problems.
For better user experience always validate on the client side
To avoid unnecessary round trips to the server,
validate form entries on the client using JavaScript
before posting them. This provides quick feedback and
makes your application feel more responsive. Always make
sure you explain your validation errors as well. If you
use complex password rules or regex patterns, include a
message to explain what the rules are to prevent user
frustration.
Always use asynchronous database operations with entity framework core
Use of asynchronous operations will allow parallel execution of the code. Entity Framework Core comes up with asynchronous extension methods that can be used to take advantage of asynchronous programming. You can find an asynchronous extension method available now for the majority of the linq methods that you use. For example, use ToListAsync() instead of ToList() to query data asynchronously. Similarly, SaveChangesAsync() is asynchronous variant of SaveChanges() method which is used to save data asynchronously.This post has been relocated to my new blog, please click the link for the updated version: 14 Tips to Improve the Performance of the ASP.NET Core Web Application (Part 1)