Soon We Can Finally Banish JavaScript to the ShadowRealm | CSS-Tricks
Briefly

Soon We Can Finally Banish JavaScript to the ShadowRealm | CSS-Tricks
"It's exceptionally likely you've seen JavaScript described as "single-threaded" at some point - that's usually pretty high up on the list of JavaScript fundamentals, alongside "case sensitive," "whitespace insensitive," and "bad at math." That is correct, in the strict "computer science" sense, but it still gets my hackles up a little whenever I see it. I mean, accurate in that JavaScript isn't multi-threaded, for sure. A script is always executed in a very linear way - top to bottom, left to right, one execution context after another, winding up the call stack and then back down again."
"It's just that you eventually come to learn about something like Web Workers, which - not to put too fine a point on this - allow you to execute JavaScript code in another thread. That's where I think "JavaScript is single-threaded" becomes a less helpful framing, because even though JavaScript isn't a multi-threaded language, a JavaScript application can make use of multiple threads."
"It's a better framing - and every bit as technically accurate - to say that a JavaScript realm is single-threaded. A realm refers to the environment where code is executed: a browser tab is a realm, and within that realm is the single thread where JavaScript is executed - the main thread. A Web Worker is a realm with a worker thread. JavaScript running in a cross-origin iframe is running in that iframe realm's main thread."
JavaScript is often described as single-threaded, meaning code runs linearly within one execution context, building and unwinding the call stack. JavaScript itself is not multi-threaded, but applications can use multiple threads through separate realms. A realm is the environment where code executes, such as a browser tab’s main thread. Web Workers create additional realms backed by worker threads. Cross-origin iframes run JavaScript in their own iframe realms, each with its own main thread. Framing the concept as “realm single-threaded” is more precise than claiming the entire language is single-threaded.
Read at CSS-Tricks
Unable to calculate read time
[
|
]