Breaking the Single-Threaded Barrier: How AtollJS Brings True Multithreading and Shared Memory to Modern JavaScript
JavaScript has had workers for fifteen years, and the way most apps use them hasn't changed: pick the expensive function, post it some data, await the result. That works, until the work isn't a function call. It's a million-row scan. It's a UI tree re-rendering every frame. It's state that both threads need live , not cloned-and-stale. AtollJS is built around four goals, each one a layer: 1.…
For over a decade, JavaScript workers have been available, yet modern apps have continued to employ them in much the same way: identifying an intensive function, sending it data, and awaiting the outcome. This approach works, but stumbles when the task at hand isn't a simple function call. It might be a task such as scanning a million rows of data, re-rendering a UI tree with every frame, or dealing with state that requires real-time updates from both threads, rather than stale, cloned data.
AtollJS is designed around four core principles, each serving as a layer in its architecture. The first principle is to share state rather than serialize it, as postMessage clones everything, which is excellent for results but not so great for continuously updating state. The second option, SharedArrayBuffer, is fast but doesn't provide a mechanism to ensure both sides are in sync.
To address this, AtollJS introduces a type. defineSharedMemory takes a specification of fixed-width fields and compiles it into a consistent byte layout, ensuring a single source of offsets for both the main thread and the worker. Importantly, this process requires no additional dependencies, as the schema engine is included within the SDK.
The second principle focuses on making worker calls feel like method calls. A worker is essentially an RPC surface, and defineWorker allows the declaration of method lists within the worker. connectWorker provides the main thread with a typed proxy over a lazily-spawned pool. This means that workers can be treated like regular methods, simplifying the interaction between the main thread and the worker.
The third principle enables results to flow back as reactivity. With shared memory, updates made by the worker can be observed by the main thread without the need for await statements. This is achieved through observe and defineTask, which allow components to subscribe to changes in shared memory, triggering updates as needed.
Finally, AtollJS allows for the rendering of UI off-thread. This means that entire applications can be pushed to the worker, where they can run in a separate thread and update the UI in real-time. The framework also supports islands, which can be mounted inside a worker to render applications within its context. This enables a seamless integration of UI rendering within the worker environment, offering a powerful tool for developers looking to optimize their JavaScript applications.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.