An array, dictionary, or list in a session?

What would be the most efficient way to store a set of values ​​in a session? I assume this is either a list / array or a dictionary. Basically, when a user selects an item from DropDownList, the value ( between 1 and N where N <= 20) is sent back to the server. I would like this value to be used as an index (if an array is used) or a key (if a dictionary is used) in a session. I do not want the value to be visible to the user, therefore why it was not stored in DDL. From what I collect, dictionaries are for key searches. However, since the scale is quite small, is there any overhead for using a dictionary that can make it less effective in this case? Each corresponding value is unique to the user, so I decided to use sessions.

Thanks for any advice.

+3
source share
2 answers

Arrays would be the best option for you here.

The dictionary can be more scalable if you intend to add a few more custom types with minimal changes to the code. Retrieving an item from a dictionary is usually a 1 jump operation. But the only overhead associated with using the dictionary is serialization. Serialization will only be used when using SQLServer or StateServer to store your sessions.

I did some benchmarking where I used BinaryFormatter to serialize an array of int, List and Dictionary.

100000 . 20 . :

int[] took 1955 ms to serialize
List<int> took 4135 ms to serialize
Dictionary<int,int> took 27917ms to serialize

, .

======================================== , , , 20 ( ) 10000000 .

int[] took 136 ms to serialize
List<int> took 184 ms to serialize
Dictionary<int,int> took 877 ms to serialize

, , , , .

, . .

+2

-, , , . .

0

Source: https://habr.com/ru/post/1731500/


All Articles