Rendering Without Copying
Read the cell buffer straight out of wasm memory from JavaScript.
The naive version returns a Vec<u8> each frame, which allocates and copies the whole grid
across the boundary sixty times a second.
Instead, expose a pointer:
#[wasm_bindgen]
impl Universe {
pub fn cells(&self) -> *const Cell {
self.cells.as_ptr()
}
}
const cells = new Uint8Array(memory.buffer, universe.cells(), width * height);
JavaScript now reads wasmโs linear memory directly โ zero copies.
One caveat that will bite you: if the Vec reallocates, wasm memory may grow and
memory.buffer is detached. Re-create the Uint8Array after any operation that could
resize.
Profile before optimizing further. In this project the render loop usually dominates the
simulation, and people spend hours micro-tuning tick() for nothing.