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 for Converge360.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube