I was wondering how wasteful is calling Session object like these (example):
string test = string.Empty;
for (int i = 0; i < 1000; i++)
{
test += Session["test"].ToString() + ",";
}
Instead of like these:
string test = string.Empty;
string test_session_value = Session["test"].ToString();
for (int i = 0; i < 1000; i++)
{
test += test_session_value + ",";
}
(that is calling HttpSessionState object and reading session from it multiple times rather than just as fewer times as possible)
Is there any performance penalty (noticeable)? How much should developer be careful with HttpSessionState object usage?
The session is fully reader when the page load, and saved when the page unloaded. So the serialize and un-serialize are happens only one time per page !
Session saved/keep the data in a
Dictionary<string,object>object.So when you made this
you actually the Session is call the Dictionary that is very fast when locate an item on his list because is use indexing.
Now what I have to notice in this code is that you make a mistake using the string that is the real costly line. To make real fast you must use
StringBuilder, but let say that is not a real code but just the way you show it.In real life I do not think that you call 1000 times per page a session variable, so it will no noticeable delay from Session. What is noticeable is that the session is lock all users until the page load and unload.
Relative: Replacing ASP.Net's session entirely