What should I use, a Dictionary with 100 entries or a class with 100 variables?
Hello there!
In short: I have created an external .json database with around 100 variables. Now, I want to load the data to my game's start-up code (executed only once in program's lifetime). But to make any use of it, first i need to somehow store it in the memory. I see two ways of dealing with that: a class or a dictionary.
To better illustrate what I mean:
Aproach 1 - Dictionary
 Dictionary<string, object> GetVariables(string fileName)
 {
     string text = Resources.Load<TextAsset>($"main/{fileName}").text;
     VariableData json = JsonUtility.FromJson<VariableData>(text);
 
     Dictionary<string, object> dictionary = new Dictionary<string, object>();
 
     for (int i = 0; i < json.items.Length; i++)
     {
         dictionary.Add(json.items[i].variable, json.items[i].value);
     }
 
     return dictionary;
 }
 
 [System.Serializable]
 class VariableData
 {
     public VariableItem[] items;
 }
 
 [System.Serializable]
 class VariableItem
 {
     public string variable;
     public string value;
 }
 
               This will return an Dictionary with all my 100 variables and values. To get the variable I need at the moment, I would simply have to write:
 Dictionary<string,object> variables = GetVariables(defines);
 int a = (int)variables["a"];
 float b = (float)variables["b"];
 
               Aproach 2 - Class
 Variables GetVariables2(string fileName)
 {
     string text = Resources.Load<TextAsset>($"main/{fileName}").text;
     return JsonUtility.FromJson<Variables>(text);
 }
 
 [System.Serializable]
 class Variables
 {
     int a;
     float b;
     string c;
     double d;
 }
 
 
               This will return an class with all 100 variables (here 4, but you get the idea). To get the variable I all I need to do is
 Variables variables = GetVariables2(defines);
 int a = variables.a;
 float b = variables.b;
 
               And here is my question: which aproach is better? Which one uses less memory, is faster etc.?
Your answer