Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:31:20+00:00 2026-06-04T00:31:20+00:00

I was wondering how wasteful is calling Session object like these (example): string test

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:31:22+00:00Added an answer on June 4, 2026 at 12:31 am

    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

    string test = string.Empty;
    for (int i = 0; i < 1000; i++)
    {
       test += Session["test"].ToString() + ",";
    }
    

    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.

    StringBuilder test = new StringBuilder();
    for (int i = 0; i < 1000; i++)
    {
       test.Append(Session["test"].ToString());
       test.Append(',');
    }
    

    Is there any performance penalty ?

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wondering how I can replace all special chars on my string like: hello this
Wondering how does an application like Process Explorer or Combo Fix detect hidden process
wondering what is the major difference between these two: onClick and mouseClick..
Wondering if there is a fast way, maybe with linq?, to convert a Dictionary<string,string>
Wondering how I can change the php session id from PHPSESSID to something else
Wondering if anyone out there has ran into this before.... I'd like to use
I was wondering how do software like GotoMeeting capture desktop. I can do a
Wondering how I can send a DELETE instead of POST/GET etc with ASIHTTPRequest. Thanks!
Wondering if there is a way to change the object on the heap that
Wondering if there is any way to get the lambda expressions that result from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.