Reputation: 21
I have some lines of code, that worked well in Xamarin.Forms, but it performs very poorly in Client-side Blazor.
This is the code:
string s = System.Text.Encoding.UTF8.GetString(Properties.Resources.city_list);
List _cityList = JsonConvert.DeserializeObject<List>(s).ToList();
the city_list is a huge list from OpenWeatherMap that is containing all the cities around the globe. Later I want to display this list as options in a select, so I would like to keep it on the client-side. The code now is very slow, it takes minutes to run it. Do you have an idea, how can I make it faster?
Thank you in advance Janos
Update: I removed the Take(20), because that is not part of the problem. I want to get the full list.
The city_list is a text file in Json format. I added it as Resource, so it is a byte array in this code.
Upvotes: 0
Views: 1191
Reputation: 41
This problem is related to very slow string parsing in Blazor. Yesterday I hit the same problem with parsing a json data of 6 mb. Even with the small json data the delays between page landings are noticable to eye. In a non-Blazor .NET application parsing is done really fast though.
So I figured out whether the problem is about json parsing or in general string parsing. I tested the below code on a reguler Console app and on Blazor WebAssembly. The difference is huge: Console(768 milliseconds), Blazor 45(seconds). Seems like this is an issue with web assembly because javascript parsing is also fast. This problem has been around for sometime. See also here. https://github.com/dotnet/runtime/issues/40386
Well this being said even though this is a very simple problem it completely invalidates Blazor as a production ready framework.
This was tested on .NET 6 both for Console and Blazor.
public class StringParsePerfTest
{
private string Text { get; set; } = "";
public StringParsePerfTest()
{
for(int i = 0; i < 10000; i++)
{
if (i % 3 == 0) Text += "|";
else Text += "a";
}
}
public void Test()
{
var start = DateTime.Now;
start= DateTime.Now;
for(int i = 0; i < 10000; i++)
{
var splitted = Text.Split("|");
}
var end = DateTime.Now;
Console.WriteLine($"{(end - start).TotalMilliseconds}");
}
}
Upvotes: 1