Skip to content

Performance issue: Why Blazor Web assembly loop is far slower than Javascript? #38505

@madnik7

Description

@madnik7

I have created two simple loops in Javascript and Blazor WebAssembly. I was going to love Blazor and had expected web assembly to be faster because it was compiled and optimized, but it was far slower than Javascript. I used .NET 6 Blazor web assembly. Did I do something wrong or Blazor is not designed for this scenario at all?

Javascript: 3 seconds

Blazor: 95 seconds

Javascript:

function Foo() 
{
  let fruit = [];
  for (var i = 0; i < 5000000; i++) {
    fruit[i] = "zzzz" + i.toString() + "yyy" + i.toString() +
      i.toString() + i.toString() + i.toString();
  }

  let counter = 0;
  for (var j = 0; j < fruit.length; j++)
    if (fruit[j].indexOf("111") > 0)
      counter++;
  console.log(counter);
}

Blazor Web assembly:

void Foo()
{
    DateTime dateTime = DateTime.Now;
    var items = new string[5000000];
    for (var i=0; i<items.Length; i++)
        items[i] = "some string some string 111" + i.ToString();

    var counter = 0;
    for (var i = 0; i < items.Length; i++)
        if (items[i].IndexOf("111") > 0)
            counter++;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor Componentsfeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssemblyquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions