Search code examples
iisasp-classicsession-variablesiis-8

Is Classic ASP Session.Variables costly?


I have read a lot of articles about session variables in Classic Asp.

I already know that accessing Request.Servervariables is costly, as the entire collection have to be fetched by the Asp Script from IIS, everytime a particular item is requested.

I was dumbly wondering if the same apply for session variables... ? Would it be a good idea to collect the few session.variables I am using when the script start, one time for all ?

The following article suggest (as I believed too) that Session.Contents does not suffer the same inefficiency as Request.ServerVariables? as the Sesison is made available one-time-for-all by IIS : Is that true ? https://web.archive.org/web/20210927201638/http://www.4guysfromrolla.com/webtech/092298-3.shtml

NOTE : My use of session variables is very very sparse, but I am looking for every bit of optimization :)


Solution

  • That's obsolutely true.

    Short answer is, if you need session variable (or application, this is the same) once, you can use it directly :

    <%=session("userLastName")%>, <%=session("userFirstName")%>
    

    If you need session variable many times, do a copy locally (this is a array in this example) :

    <%
    localAryCopy = session("myArray")
    for each tmp in localAryCopy
        response.write tmp
    next
    %>
    

    Last week, someone ask me to check why his asp classic application a a TTFB (time to first byte) to 3 second. He was using 3 sessions variables many times (3 for each embeded, for a total of about 100 loop). I just made local copy, and TTFB drop to about 50ms.