C# Blazor WASM IJSRuntime InvokeAsync Deserialisation Performance (it's quite slow)

Viewed 15

I've recently started out creating a new Blazor WASM app and I'm getting stone-walled by the performance of the in-built serialisation/deserialisation of JSON requests. For context:

  1. I'm building an app that can be used offline at any point, so I'm utilising the browser's IndexedDB (IDB) for data storage
  2. Data is downloaded from the server and saved into IDB - this process is still quite slow, so perhaps everything is related?
  3. Whenever I need to search for data, I utilise InvokeAsync to retrive from IDB and convert into C# objects - this is where I'm getting a bit annoyed (it shouldn't take so long to perform this task considering it's all 'local').

I've benchmarked the process and it takes 200ms to retrieve 1400 objects from the IDB (using an index) and push into an array ready to return - this is all pure JS at this point.

It's then taking another 3.3 secs to deserialise that array into a C# array (once returned from JS using the InjokeAsync func). There are 47 properties in the class (made up of strings, datetimes, ints, decimals and bools, so nothing obscure).

For reference, IJSRuntime is injected into the class using DI and I call the function using:

MyClass[] data = await js.InvokeAsync<MyClass[]>("RetrieveData");

I've spent many days trying to establish why it takes sooooo long, but cannot find anything concrete (which is why I am now writing this post). I've already set the JSON serialiser settings (at server level) using:

builder.Services.AddControllers().AddJsonOptions(opts => {
    opts.JsonSerializerOptions.PropertyNamingPolicy = null;
    opts.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});

I couldn't see any way of setting the same settings at client level, the builder doesn't expose those same options.

Can anybody think of anything else that I might have missed that could explain why it takes over 3 secs to deserialise an array of (relatively basic) objects? I've worked on web projects for over 10 years with jQuery ajax requests and that has always worked well (and worked quickly).

This methodology seems like quite a step back in terms of real-world performance - I know it will be picked up on quite quickly once this Blazor app goes live, so am trying my best to improve things now before it gets too complex.

Any help/comments appreciated!

0 Answers
Related