views:

202

answers:

6

The short of it is: Is it costly to check an Application Variable such as Application("WebAppName") more 10-20 times each time a page loads?

Background: (feel free to critique)

Some includes in my site contain many links and images which cannot use relative urls due to their inclusion in different paths.

Hence these includes contain frequent instances of

<img src="<%=Application("Webroot")%>images\image.gif">

Is it expensive to keep calling an Application variable like this?

Should I just put the Application value in some local variable to use where needed?

IMPORTANT NOTE:

I need my webapp to run fine on a server whether it be in the root web ("/") or in a virtual subweb ("/app").

Thanks in advance for any wisdom shared.

+4  A: 

It's cheap - very, very cheap - just a dictionary lookup. Compared with almost anything else you'll do in the app (loading something from disk or the network) this will be statistical noise.

In general though, the best thing to do if you're worried about things like this is to measure it. Arbitrarily put 10,000 calls into a page, and see how that affects performance. See how it affects concurrency as well - can you still get the throughput you need when processing multiple concurrent requests?

Jon Skeet
For the OP's benefit; in particular, it is *ridiculously* cheap compared, for example, to querying the database or returning hour http/html response to the client; for 10-20 times, it is very unlikely to cause a noticeable bottleneck, er, ever.
Marc Gravell
I'll make my answer more emphatic :)
Jon Skeet
A: 

Use Request.ApplicationPath instead (only works if your app is set as a virtual directory in IIS)

Mehrdad Afshari
A: 

Short answer - measure it and decide on your own environment. I would say it does not matter.

Longer answer - you should have the call wrapped in something anyway... Like WebConfiguration.Root. That will give you the option to do whatever optimization to it anytime in the future.

Rashack
+1  A: 

The Application object is a synchronized collection which uses ReadWriteObjectLock (an internal class that just uses the lock keyword), so if you are only reading from the collection it will be as fast as a hash table lookup as Jon mentioned, but if at the same time someone is writing to this collection, readers will block until write is complete. If you are worried so much about performance, call the indexer once, store it to a local variable and use this variable in your views.

Darin Dimitrov
+4  A: 

Just for info, another option is:

<img src="<%=VirtualPathUtility.ToAbsolute("~/images/image.gif")%>"

This works well especially in MVC, where you might write an extension method to do the job, i.e.

<%=Html.Image("~/images/image.gif")%>
Marc Gravell
Very helpful. Thank you for this tip.
Matias Nino
A: 

Hi Marc Gravell. I'm so new that I cannot up vote, and not even post a comment. So THANK you for the VirtualPathUtility.ToAbsolute. That helped me finally out (after years!!)

Henry99