14 Tips to Improve the Performance of the ASP.NET Core Web Application (Part 2)

14 Tips to Improve and Fix ASP.NET Core Performance Issues (Part 2), asp net performance, improve asp.net website performance
Photo Credit: unsplash.com/marcsm

In the first part of this article, I shared some tips to improve the performance of the ASP.NET Core performance. Here I'm sharing the rest of the tips:

Review what client scripts you are using

ASP.NET Core projects may include client script libraries which you may or may not be using some of them can cause a performance issue which may affect the SEO ranking of your website. It’s always a good idea to check what you are using, and when. Try to always use well-known libraries.

Reduce memory leaks dramatically with the "using" statement

If a type implements IDisposable, wrap the use of it in a “using” statement, so that it automatically disposes of objects properly when the block exits.

Reduce the data sent across the network

Reducing the amount of data sent across the network can improve application performance significantly. Compressing CSS and JavaScript is possible using bundling and minification. This will reduce the number of server requests and the amount of code sent across the wire.

When in production, carefully consider what you need to Log

Many people deploy to production without checking how logging is currently configured. It is always advisable to check whether your policy is to have logging on or off by default and, if on, what level you should be targeting. In addition, you should check which targets you are outputting to, what archiving strategy you have, and whether your logging infrastructure allows for async logging.

Load scripts asynchronously

Add script references at the bottom of the page, because asynchronous downloads halt when a script reference is reached. Style sheets and images can be downloaded asynchronously

Don’t assume that problems can only arise from business logic

When beginning to diagnose performance problems, we often assume the problem is in our business logic. Don’t forget that the areas of our code that provide infrastructure can cause problems as well. Areas such as HttpHandlers, HtmlHelpers, mapping, logging, or IoC frameworks are increasingly at the root of performance problems. While business logic still causes its share of problems, infrastructure code is quickly gaining in the performance problem race.

Pay attention how you are using the for loops

for is the fastest way of iterating over a collection, foreach is a little slower, and LINQ queries are slowest.

StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be

If you are in a loop and adding to a string, then a StringBuilder *could* be most appropriate. However, the overhead of spinning up a StringBuilder instance makes the following pretty dumb:

var sb = new StringBuilder();
sb.Append("Frankly, this is");
sb.Append(notMoreEfficient); 
sb.Append(". Even if you are in a loop.");
var whyNotJustConcat = sb.ToString();

Instead, use String.Join, which is typically more performant than spinning up a StringBuilder instance for a limited number of strings. It’s my go-to concat option: 

string key = String.Join(" ", new String[] { "This", "is", "a", "much", "better", "solution", "."});

The first variable of " " can just be set to "" when you don’t want a delimiter. 

For loops that do a lot of, er, looping, sure, use a StringBuilder. Just don’t assume it’s the de facto solution in all, or even the majority of cases. My rule of thumb is to add strings together when I’ve got one to five of them (likewise with String.Format if it helps with legibility). For most other cases, I tend towards String.Join. Only when dealing with a loop that limited to about 10 iterations.

Conclusion: 14 Tips to Improve the Performance of the ASP.NET Core Web Application

So far we explored 14 tips to improve the performance of the ASP.NET Core web applications. Improving the performance is very critical and ensure the continuity of the client's business. Users wants a more lightweight application which runs quicker and gives a better response time.

Do you have any other recommendation to improve the performance of the ASP.NET Core Web Application, please feel free to comment and share your opinion.

Check Out: Part 1 of Tips to Improve the Performance of the ASP.NET Core Web Application

Post a Comment

Previous Post Next Post