Performance issues in System.Text.Json methods

Sabari Anand 1 Reputation point
2022-06-28T17:51:20.947+00:00

I am facing performance issues when parsing JSON objects from a file with the following code snippets in the Blazor WASM application alone. These codes use reflection and “System.Text.Json” library. There is no performance issues in the Blazor Server application. This performance issue is faced in .NET 6.0 and the pervious framework versions in the Blazor WASM application. Please find the code snippets below.

  1. While accessing the number values from the object that has type as System.Text.Json.JsonElement using GetDouble() method. It takes time in WASM application when it is called multiple times. This method "GetDouble()" is from the System.Text.Json.JsonElement namespace and hence the issue with framework end.
     int outerLength = coordinatesArray.GetArrayLength();  
        for (int a = 0; a < outerLength; a++)  
        {  
            int innerLength = coordinatesArray[a].GetArrayLength();  
            for (int b = 0; b < innerLength; b++)  
            {  
                latitudeValues.Add(coordinatesArray[a][b][0].GetDouble());  
                longitudeValues.Add(coordinatesArray[a][b][1].GetDouble());  
            }  
        }  
    

Sample link: https://www.dropbox.com/s/57hfgwqnsll4cnm/JSONParseWASMApp.zip?dl=0
2. After getting all the number values from the JSON object, we will get the minimum and maximum value within those number values. It also takes time in WASM application and it’s just simple C# codes. The "latitudeValues" array would contain "9893" atleast latitude and longitude coordinate values.

double minLatitude = 0, maxLatitude = 0, minLongitude = 0, maxLongitude = 0;  
for (int i = 0; i < latitudeValues.Count; i++)  
{  
    double latitude = latitudeValues[i];  
    double longitude = longitudeValues[i];  
    minLatitude = Math.Min(minLatitude, latitude);  
    maxLatitude = Math.Max(maxLatitude, latitude);  
    minLongitude = Math.Min(minLongitude, longitude);  
    maxLongitude = Math.Max(maxLongitude, longitude);  
}  

Sample link: https://www.dropbox.com/s/57hfgwqnsll4cnm/JSONParseWASMApp.zip?dl=0

  1. When we process some set of data set as parameter to the razor component, we change it to a format readable (Dictionary<string, string>) in the component. But this process also induces time delay in the WASM application. Please find the code below.
             IEnumerator iterator = dataSource.GetEnumerator();  
             while (iterator.MoveNext())  
             {  
                 Dictionary<string, string> dictionaryValue = new Dictionary<string, string>();  
                 PropertyInfo[] propertyInfoCollection = iterator.Current.GetType().GetProperties();  
                 int pLength = propertyInfoCollection.Length;  
                 if (pName == null)  
                 {  
                     pName = new string[pLength];  
                     for (int m = 0; m < pLength; m++)  
                     {  
                         pName[m] = propertyInfoCollection[m].Name;  
                     }  
                 }  
                 for (int m = 0; m < pLength; m++)  
                 {  
                     var propertyValue = propertyInfoCollection[m].GetValue(iterator.Current);  
                     if (propertyValue != null)  
                     {  
                         dictionaryValue.Add(pName[m], Convert.ToString(propertyValue, CultureInfo.InvariantCulture));  
                     }  
                 }  
    
                 dataSourceCollection.Add(dictionaryValue);  
             }  
    

The performance issues from the above codes affect in my project in the Blazor WASM application. But works with good performance in Blazor Server application. Can you please provide solution for this?

Thanks in advance.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
799 questions
{count} votes