Sessions: multiple literal values and datatable

lamy

Regular
Joined
Dec 12, 2005
Messages
56
Location
under your bed
was wondering if there would be any difference between literal values and datatable placed in a session, suppose i have these

Visual Basic:
        Session("object1") = "the string"
        Session("object2") = 100
        Session("object3") = True

and these

Visual Basic:
        Dim dt As New DataTable("objects")
        Dim dr As DataRow

        dr = dt.NewRow
        With dr
            .Item("object1") = "the string"
            .Item("object2") = 100
            .Item("object3") = True
        End With
        dt.Rows.Add(dr)

        Session("objects") = dt

would it differ in performance? (probably will) what could be the best practice? (in general), just curious
 
lamy said:
was wondering if there would be any difference between literal values and datatable placed in a session, suppose i have these

Visual Basic:
        Session("object1") = "the string"
        Session("object2") = 100
        Session("object3") = True

and these

Visual Basic:
        Dim dt As New DataTable("objects")
        Dim dr As DataRow

        dr = dt.NewRow
        With dr
            .Item("object1") = "the string"
            .Item("object2") = 100
            .Item("object3") = True
        End With
        dt.Rows.Add(dr)

        Session("objects") = dt

would it differ in performance? (probably will) what could be the best practice? (in general), just curious

The less objects in the Session state - the better and faster.
 
lamy said:
so using datatable is a little slower?

Yes, and here is the good explanation why:

ASP.NET performs the serialization/deserialization of certain "basic" types using an optimized internal method. ("Basic" types include numeric types of all sizes (e.g. Int, Byte, Decimal, String, DateTime, TimeSpan, Guid, IntPtr and UIntPtr, etc)
If you have a session variable (e.g. an ArrayList object) that is not one of the "basic" types, ASP.NET will serialize/deserialize it using the BinaryFormatter, which is relatively slower.
So for performance sake it is better to store all session state data using one of the "basic" types listed above. For example, if you want to store two things, Name and Address, in session state, you can either (a) store them using two String session variables, or (b) create a class with two String members, and store that class object in a session variable. Performance wise, you should go with option (a).
 
Back
Top