Live demo
Thousands of entities moved by EasyEcs systems in your browser: .NET in WebAssembly, the C# source that runs beside it, live update times and FPS.
This page runs an EasyEcs world in your browser. Emitters drift along a curve and fire rings of sparks; drag slows the sparks down, so their color, set by speed, cools as they age; they flash where they hit a wall and fade out at the end of their life, and the emitters replace them. Move the pointer over the canvas and the sparks swirl towards it. Pick a count and watch what an update costs, and what the batch systems save over per-entity ones.
The same motion written two ways, measured live: [ForEach] over Span + SIMD 0.000 ms · [ForEach] with ref / in, one entity at a time 0.000 ms
- Entities
- 0
- ms per update, all systems
- 0.00
- FPS
- 0
Not a benchmark
Browsers do not run .NET's usual runtime, CoreCLR, yet: CoreCLR on WebAssembly is only a preview in .NET 11, and an interpreter there. This page runs on .NET's Mono runtime, compiled ahead of time to WebAssembly (Mono AOT), with WebAssembly's 128-bit SIMD, on one thread. That is slower than .NET on a server, so the update time here is not a measurement of EasyEcs. The numbers measured with BenchmarkDotNet on a real machine are on the performance page.
What runs here
The world is demo/EasyEcs.Demo in the repository: a .NET 10 WebAssembly app that references EasyEcs.Core and its
generator like any other project. Each animation frame, the page calls Demo.Step(dt) once:
- The host writes the step into the
Clocksingleton and updates a motion group:world.Update<Batched>()andworld.Update<PerEntity>()take turns, one per frame. Either one slows every spark by drag, then moves it. world.Update()runs the default group in the order the systems were added:LifetimeSystemdestroys the sparks whose time ran out,OrbitSystemmoves the emitters along their curve,BurstSystemfires a ring from each emitter whose cooldown is over until thePopulationtarget is met again,AttractSystempulls the sparks towards thePointer,BounceSystemturns them back at the walls and gives them aFlash, andFadeSystemdims the flash and removes it.Frame.Write, a[ForEach]method outside any system, copies each spark's position, heat (its speed) and light into one float array.- The page uploads that array to the GPU in one call, straight from the WebAssembly memory, and draws every spark as a soft point of light. The light fades a little each frame instead of being cleared, which draws the trails.
No call crosses between JavaScript and .NET per entity, and nothing is allocated per frame on either side: the
array lives on the pinned object heap, so its address never changes. The pointer is input, and input is data: the
page writes it into the Pointer singleton, and AttractSystem reads it on the next update.
Batch methods and SIMD
Below the canvas, the page shows what the same motion costs written two ways, measured live. DragSystem and
MovementSystem are plain [ForEach] methods, called once per spark. DragBatchSystem and MovementBatchSystem are
batch methods: they receive spans over runs of whole rows, cast them to
Vector128<float> and work on two sparks per instruction, with one scalar step for an odd spark at the end. The cast
is valid because Position and Velocity are two floats each, so a run of rows is one float array.
Both pairs do the same work and give the same result. They sit in two system groups, Batched and PerEntity, and
the host updates them in turns, one per frame, timing each: the picture does not change, both are measured on the
same sparks, and the systems hold no mode flag. WebAssembly SIMD is 128 bits wide, so Vector128 runs as hardware
SIMD here while wider vectors do not; on a server the same code can use Vector256 or Vector<float>. The larger
the count, the more the batch version saves.
The code
These are the files the demo is built from, included from the repository. Every system has one job and keeps no
state: the clock, the walls, the pointer and the population target are singletons, and a wall hit is written as data
(Flash) for FadeSystem to pick up (ECS principles).
using EasyEcs;
namespace EasyEcs.Demo;
// Components: plain data on each entity, in pixels and seconds.
public record struct Position(float X, float Y) : IComponent;
public record struct Velocity(float X, float Y) : IComponent;
// Seconds left of a total: a spark fades in, fades out and is destroyed when it runs out.
public record struct Lifetime(float Left, float Total) : IComponent;
// Present while a spark glows after hitting a wall. It comes and goes: a presence bit, the row
// stays.
public record struct Flash(float Left) : IComponent;
// A point that travels along a curve and fires bursts of sparks: Phase is where it is on the curve,
// Cooldown the seconds until its next burst.
public record struct Emitter(float Phase, float Cooldown) : IComponent;
// Singletons: one value per world.
// The walls, resized by the page with the canvas.
public record struct Arena(float Width, float Height) : ISingletonComponent;
// The step the page asked for and the number of steps so far, written by the host before each
// update.
public record struct Clock(float Dt, long Tick) : ISingletonComponent;
// How many sparks the world keeps alive: the emitters replace the ones LifetimeSystem destroys.
public record struct Population(int Target) : ISingletonComponent;
// The visitor's pointer over the canvas, written by the page: input is data the systems read.
public record struct Pointer(float X, float Y, bool Active) : ISingletonComponent;
Why is it slower than the benchmarks?
Three things a server has and this page does not:
- CoreCLR and RyuJIT. The browser runs Mono, compiled ahead of time to WebAssembly, which the browser compiles again to machine code. That code is good, but not as tight as what RyuJIT makes for the machine it runs on, and vectors stop at 128 bits.
- Threads. The page runs on the browser's main thread.
ParallelSystemsand[ForEach(Parallel = true)]need more than one, so the demo leaves them off. - A quiet machine. The page shares its thread with drawing, the browser and every other tab, so the update time moves from frame to frame.
The data layout, the generated jobs and the zero allocations are the same in both: what you see here is the shape of the work, and the performance page is its speed.
Run it yourself
The page loads the runtime from docs/public/demo/_framework, published from demo/EasyEcs.Demo by
docs/scripts/build-demo.sh (bun run demo:build in docs/), which installs the wasm-tools workload that Mono AOT
needs when it is missing. Change a system, run the script, and bun run dev serves the page with your code. The
same systems run unchanged in a console app or a server: only Demo.cs, the host, knows about the browser.
Related
Last updated on
How is this guide?
Getting started
Build a small ECS simulation in C# step by step, from dotnet new console to components, tags, queries, systems, singletons, system groups and the update loop.
ECS principles
How to design features with an ECS: eight rules for entities, components, singletons and systems, the analyzer checks behind them, and a checklist.