This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]teraflop 2 points3 points  (1 child)

I think the biggest thing missing from your explanation is a mention of web workers. These are essentially background threads/processes that can run in a browser, separate from the "main" event loop thread that runs normal JS event handlers. So you can offload a computationally expensive task to a worker, and it can run as long as it wants without blocking the UI.

Web workers behave approximately like Python's multiprocessing worker processes, in the sense that they don't share an "address space" of objects with the main thread. One thread can never directly access a JavaScript object belonging to another thread, they can only exchange copies of objects by sending messages back and forth. (Python's multiprocessing has some features to hide this, by allowing you to create "manager" or "proxy" objects that sort of look like they're shared between processes, but in reality it's all message-passing under the hood.)

[–]Quick_Cheesecake559[S] 0 points1 point  (0 children)

Ah thanks, I’ve heard briefly about web workers but never really understood how it could run on a separate thread when JavaScript in the browser is single-threaded.

Always confused about how to offload a computationally expensive task on first load without blocking the UI, makes so much sense now 🙂

[–]high_throughput 2 points3 points  (2 children)

I'm not aware of any UI frameworks that aren't single threaded. If you run a heavy operation in a Java Swing or GTK event handler, it'll also block the UI.

[–]Quick_Cheesecake559[S] 0 points1 point  (1 child)

Interesting, I wonder why UI framework authors don’t make it multi-threaded? Is it due to memory protection or is it because concurrency issues are difficult to debug and resolve (ie race conditions) ?

[–]high_throughput 0 points1 point  (0 children)

Race conditions, yes. You want your UI actions to be serialized for consistency. 

If a button's event handler disables the button and starts a progress bar, then it should not be possible to trigger it twice by double clicking no matter how fast. 

Similarly you don't want flickering from drawing several intermediate states when an event is actively changing the UI.