A hands-on experiment that moves a slow C# method out of the Blazor WebAssembly UI thread and measures the difference. A prime-counting loop stalls the page for about 3 seconds in place and only 0.05 seconds inside a worker.
Key points
- Blazor WebAssembly runs C# on the browser's rendering thread, so a long loop blocks drawing completely.
- Task.Run does not help without WebAssembly threading: it just queues the work on that same thread.
- The demo offloads the method through SpawnDev.SpawnJS.WebWorkers with Workers.TaskPool.Run().
- A worker boots a second copy of the app, so Program.cs runs there too; the root component is added only when JS.IsWindow is true.
- Each worker has its own memory: statics are not shared, inputs travel as arguments, large arrays need transferable objects.
- Counting primes up to 6,000,000 stopped the clock roughly 3 seconds in place versus about 0.05 seconds in the worker.
Why it matters
For teams that cannot move a CPU-heavy calculation to the server, this is a workable client-side escape hatch, but it is an architectural decision: you start deciding which methods are UI-independent enough to send across a thread boundary, and that reshapes how the feature is written. The measurement comes from one desktop machine in headless Chromium, so treat the exact figure as a ceiling rather than a promise.
SpawnDev.SpawnJS is a community package, and its release history and maintenance belong on your pre-production checklist. The obvious alternative is the same calculation behind an API endpoint: more latency, less client memory and download weight. Plenty of products mix both, keeping heavy rare work on the client and frequent work on the server. That combination is usually where this pays off.
Real-world use
- A Blazor WebAssembly page where a button starts a long calculation and the page must stay scrollable and clickable meanwhile.
- Screens that need a live clock or progress indicator to keep ticking while a result is being computed.
This summary was generated by the agent from the original post on DEV Community: #blazor. The full text and all rights belong to its author and publisher. Go to original