DEV Community

Discussion on: JavaScript, What Are You?

Collapse
 
rkfg profile image
rkfg

You're mistaking concurrency with parallelism. They're orthogonal really. A concurrent app may or may not be parallel, it utilizes small blocking routines that are picked up for execution by a scheduler. These routines may be executed on one or more threads and each only blocks the thread for a short while. It's also called "cooperative multitasking" (as opposed to "preemptive multitasking", where the OS decides when the thread is interrupted and which one runs next). So JS is concurrent AND single threaded. You can see it doing several tasks "at once", though really each one does a small step and returns to the event loop so that the next task can do its step. If you only use one thread, you don't need synchronization primitives you mentioned. Even if you use many threads with concurrency you may not need them if you don't have any global state (i.e. the tasks are "pure" in functional sense).

Collapse
 
martinhaeusler profile image
Martin Häusler

Dude, stop splitting hairs here. For all means and purposes in real life, concurrency and parallelism are synonyms. Ask 100 programmers what the difference is and 99 of them will tell you they are identical. And JS is definitly not parallel. JS even managed to deceive people into thinking that it is multi-threaded because they deliberately use the term "asynchronous" everywhere. I call that false advertising.

Thread Thread
 
rkfg profile image
rkfg

They might be synonyms for some people but it's important to understand the fundamental differences. There's no false advertising here, if people misunderstand the terminology it's not a JS fault. JS is not parallel, that's absolutely true. It's concurrent. Concurrency is a design decision, it doesn't have anything to do with multitasking, parallel execution, threads etc. It's just small blocking callbacks on steroids if you wish. Everything is put into a callback and a supervising scheduler (a part of the same program, not the OS) chooses which one to execute.

If some people want to believe in something that's not there, it's a sect, cargo cult, religion, you name it. I'd like to dig deeper and know the underlying truth instead of marketing bs or things pretending to be what they're not.

Further reading, if you're interested: actor model, greenlets, coroutines (boost::asio or goroutines are good examples). There are different approaches to concurrency while parallelism isn't that diverse — it's just code that runs on different threads and managed by the OS. The context switching is also quite expensive, that's why concurrency is this popular, you can achieve almost-multitasking for almost no cost.