Skip to main content
ENGINEERING|

Let Your Browser Do the Walking

Rust, WebAssembly, and a perfectly good CPU currently animating your loading spinner.

We have put a remarkable amount of engineering into teaching web applications to wait politely. Skeleton screens. Progress indicators. Optimistic updates. Entire design systems devoted to making the interval between a question and its answer feel less irritating.

Sometimes the server really does need to do the work. Sometimes there is also a perfectly capable computer on the other side of the request, currently animating three grey rectangles.

I have been spending time with Rust, WebAssembly, and algorithms for problems where resources are limited and rules interact. What interests me is how local computation changes the way someone can work with those problems.

A user changes an assumption. The application evaluates the consequences. They try another version, compare it with the first, and keep the parts they prefer. Calculation becomes part of the conversation they are having with the problem.

That is a much more interesting product capability than a faster loading spinner.

A small city with a reversible bad decision

Living City is a traffic simulation I built around a small river town. Close the main bridge and watch the traffic adapt. Queues build, vehicles seek other routes, and limited capacity elsewhere becomes rather less theoretical.

Then rewind to before the closure, make a different decision, and compare the two outcomes side by side at the same simulation time. The original stays available while you explore the alternative.

Municipal decision-making with an undo button. I appreciate the ambition.

The timeline restores saved snapshots and replays the simulation to the selected moment. That gives you a shared starting point for two decisions, with the consequences calculated locally in the browser.

One detail matters more than the moving cars: both scenarios receive the same external travel demand. The randomness that determines who wants to travel when is separate from vehicle behaviour. Closing a bridge must not quietly change the population of trips you are comparing. Otherwise, your apparent improvement might just be a different Tuesday.

The comparison also accounts for trips that have not finished. Looking only at completed journeys can make a badly congested scenario appear efficient: the slowest travellers have conveniently failed to arrive in your statistics.

The traffic model is deliberately simplified. It gives the user a way to explore consequences under explicit assumptions.

Try Living City - close the main bridge, let the consequences develop, then use the timeline and comparison controls to explore an alternative. The demo is designed for desktop screens. Source code.

From exploring consequences to searching for choices

Living City lets you ask: “What happens if I close this bridge?” A solver lets you ask: “Which changes could improve the situation while respecting the constraints?” The next step would be to let a search propose interventions and use the simulation to evaluate them.

For a resource allocation problem, the model describes available choices, constraints, and how to compare candidate solutions. Hard constraints define what is allowed. Preferences describe what makes one allowed solution more useful than another.

That distinction belongs in the implementation. If an absolute requirement is represented as merely an expensive preference, enough small improvements can accidentally buy permission to break it. Rejecting invalid candidates, or ranking hard violations separately from preferences, makes the priority explicit.

Building a new solution and repairing an existing one also call for different objectives. Existing decisions carry knowledge, negotiations, and commitments that your score function may never have heard of. When a resource becomes unavailable, a useful repair preserves as many of those agreements as possible. Reorganising everything for a slightly better score would be an impressive answer to the wrong question.

The cheapest calculation is often the one you stop repeating

Rust gives me useful control over data representation, allocation, and the code that runs in a tight loop. The first useful optimisation question is still: why are we doing this much work?

Suppose a search examines thousands of small changes. Recalculating the entire score after every change repeats work. If one assignment affects two resources and a limited interval, an incremental scorer can inspect those parts. The saving comes from reducing the work before the compiler gets involved.

Living City offers a concrete example in its routing. It builds reverse Dijkstra trees per destination and caches them, so vehicles travelling to the same destination can reuse route calculations. Trees are rebuilt when costs or road availability change. Rerouting work is also staggered and budgeted, so closing a bridge does not require every affected route to be recalculated in a single simulation tick.

The same reasoning applies to repeated scans: build an index once, then ask it for relevant candidates. Compact occupancy data can make conflict checks cheap. The responsibility is to capture every affected dependency; later, a full reference calculation can check that the shortcut preserves the answer.

Compiling a quadratic algorithm to WebAssembly leaves it quadratic. The loops may run faster. Their enthusiasm for multiplying has survived the journey.

Give the search a budget and an honest answer

Interactive search needs a stopping point. Explore a neighbourhood of changes, keep the best candidate found, and return within a defined work budget. Let the user inspect the result and decide whether further exploration is worthwhile.

The result needs more information than a candidate and a cheerful success flag. Did the search reach its iteration limit? Did it stop finding improvements? Are there still constraint violations? Which decisions changed?

Failure to find a solution within a budget does not prove that none exists. A locally good result does not establish a global optimum. The interface should describe what the search actually achieved.

Reproducibility makes those results easier to investigate. Fix the input, configuration, algorithm version, random seed, and iteration order. With a deterministic implementation and a fixed iteration budget, you can replay the work. A wall-clock timeout alone gives different machines time to explore different amounts of the problem.

Keep the engine busy and the interface available

Living City's simulation core is written in Rust, compiled to WebAssembly, and connected to JavaScript through wasm-bindgen. React handles controls and panels; WebGL2 draws the town. Three architectural choices keep those jobs from getting in each other's way.

The simulation runs inside a Web Worker. The interface sends commands and remains available while the engine advances the city. Compiling to WASM alone would still leave a long synchronous calculation blocking whichever thread called it.

Data crosses the boundary in compact frames. The vehicle buffer is transferred from the worker to the interface and returned for reuse, avoiding a copy at that boundary. There are no per-vehicle JavaScript-to-WASM calls or per-frame JSON. Closing a bridge does not require serialising the entire city and sending it on a sightseeing trip through React.

Rendering follows its own pace. The simulation advances in fixed steps, while the renderer interpolates vehicle positions between frames. A slower draw rate affects how smoothly the cars move on screen, while the simulated sequence of events stays the same.

That separation supports both responsiveness and meaningful comparison. Two scenarios can advance to the same simulation time even when the browser cannot draw every intermediate moment.

AI makes more experiments affordable

AI-assisted development makes more of these experiments affordable: alternative indexes, candidate selection strategies, incremental scoring, and generators for difficult inputs. An idea that would otherwise remain a note can become an implementation worth testing.

An assistant can explain an unfamiliar technique, challenge an assumption, or help construct a case that breaks a shortcut. The useful gain is being able to investigate more options carefully.

Living City supplies concrete contracts: restoring and replaying a snapshot should reproduce an uninterrupted run; branching should leave the original untouched; and different interventions should preserve identical external demand. Its tests cover those behaviours. They give an assistant clear boundaries for an optimisation.

The evidence still needs to come from outside the proposed optimisation. Keep a straightforward reference calculation for small cases. Compare the incremental scorer with a full recalculation. Where an exact answer is feasible, enumerate tiny problems and compare results. For heuristics, measure solution quality as well as execution time.

A useful task for an assistant is concrete: implement an incremental score for one change, preserve these invariants, and compare it with this independently specified reference across generated inputs. Review the reference and the invariants with the same care as the implementation. Two functions built from the same misunderstanding can agree beautifully.

Tests also need to establish that the algorithm actually did something. A search that proposes no changes will pass “never moves a fixed assignment” and “never introduces a new violation” with outstanding consistency. Include cases where an improvement is known to exist, and check that the search finds one.

Benchmark representative cases in the browser, including awkward ones, and record the build and dataset. Measure the quality of the result alongside execution time, so a faster algorithm cannot quietly win by doing a worse job.

The resulting product can run an ordinary compiled algorithm with no language model in the calculation path. AI helped build and investigate it. The deployed behaviour is defined by code, inputs, and rules that can be inspected and tested.

Make room for exploration

The product possibilities are broad: capacity planning, allocation, configuration, forecasting, and scenario comparison. A user can preserve a baseline, change an assumption in a separate scenario, inspect the consequences, and bring back a proposal.

Local computation gives those experiments room to happen without a server round trip for every change.

In a shared application, the server validates an accepted proposal against current data and permissions. The browser can explore a working copy; committing a decision must account for what other users have changed. A portable Rust core can let both sides use the same rules.

What interests me is the combination: explicit rules, algorithms that avoid unnecessary work, a portable computational core, and AI that makes more careful experiments affordable.

Put those together, and the browser becomes a place where people can work through consequences before committing to a decision.

I would like more software to let me try the idea while I still remember why I had it.