A recording of the main thread before and after the move. JavaScript gives you a dozen ways to solve the same problem, and the platform quietly added a better one while most of us were not looking. This post walks through that better way and the reasoning behind it.
Sample footage: Sintel, an open movie by the Blender Foundation, released under a Creative Commons licence.
The problem in one snippet
The first version of our code worked on a fast laptop and failed on a slow phone. Nothing was wrong with the logic; the timing assumptions were simply never written down, so nobody noticed when they stopped being true.
- Measure on a throttled CPU, not your laptop.
- Clean up listeners in the same place you add them.
- Treat every network call as cancellable.
- Log the slow path, not just the error path.
Once the pattern is in place, the rest of the codebase gets simpler. Callers no longer need to remember cleanup, and the error paths are handled in one spot instead of five.
If you try this on your own project, start with one page or one service rather than the whole codebase. A small experiment gives you real numbers to argue with, and it is easy to roll back if the idea does not fit.
We have been running this approach for several months now. The initial setup took about a day, and the ongoing cost is close to zero once the team knows where to look.
None of this is new, and that is the point. The techniques that last are usually the boring ones that are documented well and supported by the platform itself.
As always, the examples are simplified to fit on a page. The production version has more error handling and logging, but the shape of the code is the same.
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
Simplicity is prerequisite for reliability.
Edsger W. Dijkstra, EWD498, 1975
Is there a repo with the full example? Happy to open a PR with a TypeScript version.
Counterpoint: for small teams the simpler approach in the first section is usually enough.