hraness
Theme
Appearance

saved

Threads with shared mutable objects are a nightmare on JavaScript's event loop

by José ValimXpublished

Hraness republishes this public post from a saved copy. The post is the author’s own words.

José Valim @josevalim

If by threads with shared objects, you mean threads with shared mutable objects, that will be a nightmare to write, maintain, and debug on top of JavaScript's event-loop, even for coding agents.

Rust has an ownership and type system specifically designed to make shared-memory concurrency safer, and I can say from firsthand experience that coding agents still struggle with it.

A huge amount of existing code relies on synchronous JavaScript running to completion within an agent. If ordinary objects could suddenly be accessed concurrently, two handlers running on different threads could observe and mutate the same object at the same time. Code that is safe today because there cannot be an interleaving between two synchronous operations could suddenly require synchronization.

That's most likely why existing JavaScript concurrency mechanisms generally rely on isolated object heaps with message passing, or explicitly shared memory such as SharedArrayBuffer, rather than making arbitrary JavaScript objects concurrently mutable.

I'd say changing such fundamental properties about the language is effectively making it worse, not better. Sound types plus AoT compilation are +1s, though.

Jarred Sumner (@jarredsumner):

JavaScript was never designed for the server. If that doesn't change soon, JavaScript on the server will be replaced by Rust.

The fix needs to happen in the engine. Sound types. Ahead-of-time compilation. Threads with shared objects.