dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.