News

Blazor Guru Details Best Performance Practices

Microsoft's point man on the Blazor project -- and basically the creator of the project -- Steve Sanderson, is detailing performance best practices for the web development framework that uses C# instead of JavaScript.

It does that through the use of WebAssembly in the client-side component of the Blazor framework, aptly named Blazor WebAssembly.

Sanderson works on Microsoft's ASP.NET Core team where Blazor lives, and since basically introducing Blazor to the world back in 2017 has worked to further the project with many new innovations (including an experiment in which he replaced Blazor's renderer to render to native Flutter controls).

Among those efforts is the GitHub-based "ASP.NET Core Blazor WebAssembly performance best practices."

The site, maintained in conjunction with Pranav Krishnamoorthy, says:

Blazor WebAssembly is carefully designed and optimized to enable strong performance in almost any realistic application UI scenario. However, producing the best results depends on developers using the right patterns and features. Two aspects to consider are:

  • Startup time. Your application needs to transfer a .NET runtime into the browser, so it's important to use features that minimize the application download size.
  • Runtime throughput. Your .NET code runs on an interpreter within the WebAssembly runtime, so CPU throughput is limited, and in demanding scenarios you will benefit from optimizing rendering speed.

It goes on to discuss in detail several best practices having to do with:

  • Optimizing rendering speed including:
    • Avoid unnecessary rendering of component subtrees
    • Virtualization
    • Creating lightweight, optimized components
    • Don't trigger events too rapidly
  • Optimizing JavaScript interop speed, including:
    • Avoid excessively fine-grained calls
    • Consider making synchronous calls
    • Consider making unmarshalled calls
  • Minimizing application download size, including:
    • Intermediate Language (IL) trimming
    • Intermediate Language (IL) linking
    • Use System.Text.Json
    • Lazy load assemblies
    • Compression
    • Disable unused features

The UK-based developer today (Sept. 30) tweeted about the project:

Blazor Performance Tweet
[Click on image for larger view.] Blazor Performance Tweet (source: Steve Sanderson/Twitter).

The specific technique that he said many people don't know about concerns Defining reusable RenderFragments in code.

It reads in part: "You may be factoring out child components purely as a way of reusing rendering logic. If that's the case, it is still possible to do that even without declaring actual components. For example, in any .razor component's @code block, you can define a RenderFragment that emits UI and can be called from anywhere."

The tip received kudos from several developers and followers of the Blazor guru, including:

Wow & thank you. I love this type of documents. Lot's of info with small practical examples. And you're right, I didn't know I could define reusable RenderFragments in code. I start always without using any component (made by myself) and only refactor it in a separate component

In response to another developer who asked if the tip applied equally to the server-side component -- aptly named Blazor Server -- Sanderson said, "Yes, but not as much because it’s running on the full CoreCLR with JIT, so is way less CPU bound."

The GitHub project has received more than 7,900 stars as of this writing, being forked some 19,800 times.

For more information, Sanderson's own GitHub site is here, while his personal blog is here -- although he tweets more than he blogs, as the most recent blog post was from January, titled "Using gRPC-Web with Blazor WebAssembly."

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube