Blazor Web assembly string injection is slow
rootflood opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
pass string from js to .net and .net to js is very slow
Microsoft.JSInterop.IJSRuntime Js;
var JsIn = (JSInProcessRuntime)Js;
string str = new string(new char[999999]);
{
var JsFuncs = JsIn.Invoke<IJSInProcessObjectReference>("eval", @"({
Func: function (str) {
str.toString();
return str;
},
})");
Stopwatch stopwatch = Stopwatch.StartNew();
str = JsFuncs.Invoke<string>("Func", str);
stopwatch.Stop();
Console.WriteLine("method1: "+ stopwatch.ElapsedMilliseconds);
}
Console >> method1: 20910
Describe the solution you'd like
solution is using byte array in .net and uint8array in js
Microsoft.JSInterop.IJSRuntime Js;
var JsIn = (JSInProcessRuntime)Js;
string str = new string(new char[999999]);
{
var JsFuncs = JsIn.Invoke<IJSInProcessObjectReference>("eval", @"({
Func: function (uint8arr) {
var str =new TextDecoder().decode(uint8arr);
str.toString();
return new TextEncoder().encode(str);
},
})");
Stopwatch stopwatch = Stopwatch.StartNew();
str = System.Text.Encoding.UTF8.GetString(JsFuncs.Invoke<byte[]>("Func", System.Text.Encoding.UTF8.GetBytes(str)));
stopwatch.Stop();
Console.WriteLine("method2: " + stopwatch.ElapsedMilliseconds);
}
Console >> method2: 615
Additional context
No response
@rootflood thanks for contacting us.
While you might have found a faster way to do this, it's not a problem we've seen people report in sufficient numbers for us to believe we need to investigate or invest more time on optimizing this.
Also note that you don't provide any details on your performance environment, which invalidates any number you provide, as we can't accurately rule out that there are other factors interfering with the results.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.