C# – ASP.net performance code behind vs app_code

asp.netcperformancewebsite-performance

I am wondering about performance of a web site/application when there is code in the code behind files rather than when the code is moved to separate files in the App_Code folder.

My understanding is that the code behind gets compiled and executed every time they are requested by the user. If this is not the case, please let me know. Also, it is my understanding that the code in the App_Code file gets compiled once.

With these understandings, it would seem that moving code the App_Code folder would speed up requests. I understand that there wont be any significant gains until you have lots of files and lots of requests very frequently. However, will this cause any performance value?

Also, by performance I am talking about speed. Specifically speed of client side load. And I know there are other factors that can increase speed on the users end, but I am specifically asking about the above situation.

Best Answer

Your understanding is incorrect. Both the App_Code and aspx files are compiled just once and then cached. Individual files are only compiled when used (at least that's the most common case).

There is a performance hit when a page is first hit, as it has to be compiled, if this is a problem web sites may not be for you.

See Understanding ASP.NET Dynamic Compilation:

In order for your Web application to service requests, ASP.NET must first parse and compile the code of your Web application into one or more assemblies. When the code is compiled, it is translated into a language-independent and CPU-independent representation called Microsoft Intermediate Language (MSIL). At run time, MSIL runs in the context of the .NET Framework, which translates MSIL into CPU-specific instructions for the processor on the computer running the application.

ASP.NET dynamic compilation enables you to modify your source code without having to explicitly compile your code before you deploy your Web application. If you modify a source file, ASP.NET automatically recompiles the file and updates all linked resources. The IIS server does not have to be restarted for the changes to take effect unless the <processModel> section has been changed...

By default, ASP.NET Web pages and code files are compiled dynamically when users first request a resource, such as an ASP.NET page (.aspx file), from a Web site. After pages and code files have been compiled the first time, the compiled resources are cached, so that subsequent requests to the same page are extremely efficient...