5 ms·
JS already has a proper concurrency model which is the event loop. What it doesn't have is a model for parallelism. It's important to understand the difference!
by preek 9y ago
JS already has a proper concurrency model which is the event loop. What it doesn't have is a model for parallelism. It's important to understand the difference! It can be hard to grasp at first, because few people make the proper distinction.
If you're new to the subject, don't take my word on it, but watch the talk "Concurrency is not parallelism" by Rob Pike[0]. It's highly recommended.
Learning a language that makes a proper distinction will help - I got it through Clojure, but there's certainly others like (obviously) Golang.
0. https://youtu.be/cN_DpYBzKso https://youtu.be/cN_DpYBzKso
- pizlonator 9y agoEven loop is not concurrency. Just because concurrency and parallelism are different does not meant that concurrency and event loops are the same.
- christophilus 9y agoYou can build concurrency on the event-loop, though, which is what the OP was probably referring to.
- pizlonator 9y agoNot quite. Consider a component in your code that does this: for (let blah of things) foo(blah); In an event loop, if things has a lot of stuff in it, this can block the event loop. Other things in the event loop won't be able to run until this completes. With threads, this just works.
- enoah_netzach 9y agoThat is parallelism, not concurrency. Concurrency: two functions can run one after the other while they are non blocking for the callers (e.g. hyperthreading). Parallelism: two functions can run at the same time (e.g. multiple processors).
- pizlonator 9y agoYour definition of concurrency is not correct. Concurrency usually implies that the work is chopped up into very small interleavable bits. A while function call with a loop of unbounded length being the default granule of interleaving is definitely not part of the definition of concurrency. The term "concurrency" subsumes both event loop style concurrency and two-cores-running-code-at-the-same time concurrency.
- Jtsummers 9y agoYes, it is. The JavaScript event loop is a very lazy scheduler than does no preemption. It's a very primitive concurrency model. It's up to the users of the language to break their tasks down into sufficiently small components or yield to allow for interleaving of work.
- deleted 9y ago[deleted]
- skybrian 9y agoCreating async tasks that can run concurrently and deliver progress via events is a concurrency model.
- loftyal 9y agoHow? Explain please. https://en.wikipedia.org/wiki/Concurrency_(computer_science) https://en.wikipedia.org/wiki/Concurrency_(computer_science) "order-independent components", which makes sense for the Event loop.
- pizlonator 9y agoI draw the line at whether the interleaving granule is under the programmer’s control or not. For example, I can say that when a task executes on an event loop, no other tasks are executing concurrently to it because the event loop executes tasks sequentially.
- loftyal 9y agoOk you draw the line there, but definitions are supposed to be commonly understood, that's how language works...
- pizlonator 9y agoYeah, I agree it would be better if people in the event loop community learned to use the word “concurrency” correctly.
- slackingoff2017 9y agoEvents and asynchronous operations are sufficient enough for a concurrency library to be built on top of JS with minimal changes. I wouldn't consider what JS has now a reasonable concurrency model but the asynchronous event handling could serve as a starting point for a source of events that run on multiple threads.
- pizlonator 9y agoThat was the state-of-the-art in OS concurrency in the early nineties. Sometimes, you would have to reboot your computer because some app forgot to yield the event loop. It's not as bad if you have to restart your app because some task forgot to yield the event loop, but it's still bad. Threads solve this problem comprehensively because threads get to make progress regardless of whether or not other threads are in a loop.
- vardump 9y ago> That was the state-of-the-art in OS concurrency in the early nineties. Many systems had pre-emptive multitasking in seventies and eighties. I think Amiga was the first implementation for consumers. Released in 1985. https://en.wikipedia.org/wiki/Amiga_1000 https://en.wikipedia.org/wiki/Amiga_1000
- stupidcar 9y agoYou've got to love computer scientists: "Hey, there's this word 'concurrent', which means to happen at the same time, what should it mean in our field?" "How about, 'happening in independent processes'?" "Great! There's this other word, 'parallel', that means coexist alongside but be structurally independent. What should that mean?" "How about, 'happening at the same time'?" "Fantastic! OK, let's go to the pub." Hard to believe that people get confused.
- millstone 9y agoThe event loop doesn't allow you to do a computation concurrently with handling user events, so it's not at all a proper concurrency model.
- Jtsummers 9y agoIt's a proper concurrency model, it's just not an efficient one when it comes to execution. Concurrency has (at least) two components: design concurrency, execution concurrency. The event loop mechanism in JavaScript allows you to do effective concurrent design. It does not provide for concurrent execution. This is the primary distinction in academic CS between concurrency and parallelism.
- millstone 9y agoThe event loop mechanism in JavaScript is not well suited to designing OR executing a computation concurrently. Try replacing a for loop in numeric code with anything that allows for events to be handled. It's extremely awkward.
- coldtea 9y agoSurprisingly, that's not what concurrency model means. What you describe is parallelism.
- pizlonator 9y agoConcurrency generally means preemption. Event loops have no preemption. It's not correct to use parallelism to mean preemption, as you seem to be doing.
- Jtsummers 9y agoIn CS, parallelism is simultaneity. Two things are parallel if they occur at the same time. Concurrency is design, which may allow for parallelism. And may allow for preemption. Preemptive or cooperative concurrency is a design decision, both are concurrency.
- dboreham 9y agoIn case anyone is wondering: this is nonsense. JS the language has about as much to say about concurrency as C does.