content
content copied to clipboard
Add guide for memory sharing in JS
What is the new suggestion?
A new guide section introducing Atomics, SharedArrayBuffer, and possibly workers/postMessage.
Why is it important or useful?
We have pages like SharedArrayBuffer and Atomics that kind of assume you have the background knowledge of how memory sharing works, what a "JavaScript thread" is (Is it its own realm? Does it have its own event loop? etc.), or what JavaScript memory representation looks like. For example:
The
wait()andnotify()methods are modeled on Linux futexes ("fast user-space mutex") and provide ways for waiting until a certain condition becomes true and are typically used as blocking constructs.
This sentence is basically impenetrable to me. More guidance needed.
Other supporting information
I think we can add it as a section under https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
Yes, excellent idea. As and aside, it might also be good to have docs on how to move buffers and other resources efficiently. Specifically cooperative sharing of resources by transferring ownership rather than mediating ownership via a mutex/semaphore. The sort of thing you see when you call postMessage or structuredClone() with transfer of a view buffer. This would have been useful when I was learning about readable byte streams.
I think we can add it as a section under https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
When I look at that, I consider that the Memory Management page is already very long, and I don't believe that it would suffice to have a short page about Atomics/SAB/memory sharing, if we want people to really have a good idea about how to use it. As a result, I expect it to just overload the reader.
Maybe it would be better to have a section with a short high-level paragraph touching upon its use, and then link to a dedicated page?
Memory management is long, but not too long to sneak in another section. Frankly I have very little idea about how much detail we want to go into about memory sharing, but if it turns out to be a lot, a new page definitely works.