1

I just picked up a project from previous developer, I notice in his code he load data into session variable in master page, and store it into master page's local variable for inherted pages to use it.

i.e:

in master page:

public class Master_Page
{
   public string some_data = string.empty;

    public void Page_Load()
    {
        some_data = Session["some_data"];

    }
}

public class Inherted_Page : Page
{
    public void some_method()
    {
        Response.Write( ((Master_Page)Master).some_data );
    }
}

I wonder if that's because access to session variable takes longer than local variable? And I wonder if that's the same for Request string?

2
  • Some code examples of this would be useful, please :-)
    – dash
    Dec 8, 2011 at 0:19
  • there, should be something like above :)
    – King Chan
    Dec 8, 2011 at 1:15

2 Answers 2

1

Accessing the Session object is slower than accessing an instance variable, because it requires a hash table lookup.

I believe the Request object also uses a hash table.

Having said that, the performance gains are pretty low unless you're doing a lot of them, since hash table lookups tend to be O(1), particularly for the small size that's typical for Session or Request (I suspect that computing the hash for the key probably takes longer than the lookup itself).

0

If you do not need this data to keep and follow your user to the full session, but only needed for the page, for one load, then the session data is bad and slower way to store values, and potential can lead to bugs.

So if you need to use your some_data only for this page life, only to transfer data from master page to children page, then you must go with the variables on page.

If you need some variables that follow your user on his session, then you go with the session data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.