I keep reading about JavaScript implementations being single threaded, so I was wondering if it means that it takes a single logical thread of a CPU? or are software threads completely different from hardware threads?
-
https://medium.com/@theflyingmantis/javascript-single-threaded-non-blocking-asynchronous-concurrent-language-ffae97c57bef – epascarello Sep 03 '20 at 16:05
2 Answers
(V8 developer here.)
JavaScript implementations being single threaded
That's not quite correct. JavaScript as a language is single-threaded. That means it doesn't give you, the programmer, a way to say "please start a new thread running this code here in parallel to my other code". Only one of your functions will execute at a time. A new function (e.g., a scheduled callback) can only start executing when the currently executing function has returned.
(Web workers are not a contradiction to this. They do execute concurrently with your main code, but that's not a JavaScript language feature. Instead, the browser environment gives you a way to spawn an independent second instance of a single-threaded JavaScript execution environment.)
JavaScript implementations, i.e. engines, are free to use as many threads as they want for background tasks. For example, when you use asynchronous DOM features (like the fetch
API), then typically another thread takes care of doing the work (in this case, the fetching) in the background (typically on another CPU core). Once the result is available, a callback is scheduled, which has to wait until the single main thread is free to execute it. For V8 specifically, I can tell you that it also uses background threads for parsing and compiling JavaScript code, as well as for garbage collection. (Other engines probably do that too, but I don't know them as well.)
are software threads completely different from hardware threads?
Well, a software thread is a "thread"/strand/sequence of execution that wants to run, and a "hardware thread" is the hardware's ability to execute it. Personally, I think "hardware thread" is a confusing misnomer, and it would make more sense to call it "(logical) CPU core" instead; at any rate it amounts to the same thing.
So yes, a single-threaded program will run on one hardware thread (or not run at all). JavaScript programs themselves are single-threaded (this is defined by the language), but engines running the program typically use several threads (i.e., several software threads running on several "hardware threads"/CPU cores).

- 34,271
- 7
- 59
- 74
A software thread, at any given time, is executing at most in only a single CPU core. I have included the "at most" because a software thread can be in a bunch of states other than execution in which it isn't running (it can be waiting, sleeping, dead...).
You have no guarantees that a software thread executes always on the same CPU core. Although, for performance reasons (mostly related to caches), your operating system thread scheduler will probably try to execute a thread always on the same core.
Hardware threads (or logical cores, because the operating system sees them as if they were real hardware cores) are a feature of some processors that allow multiplexing the execution of several threads on the same core under some condition. For example, you can execute two threads in a single core if you have twice the registers and both are using different parts of the core or waiting (e.g. one is adding numbers while the other is doing logical operations) at any instant of time.
In summary, your operating system is, probably, using the same core for your JavaScript engine most of the time it is executing, but you really shouldn't rely on this assumption for anything.

- 498
- 3
- 9
-
Thanks for sharing! Does this mean that if there are more software threads than physical ones, the OS will be switching between them? and if so, how does we ensure that our Node.js is prioritized over other threads that may be running on our server? Thanks again! – Satvir Sandhu Sep 03 '20 at 16:22
-
1Yes, indeed I would bet that right now you are running more software threads than hardware threads. Your operating system switches threads all the time using some heuristic to choose which thread should execute the next. If you are on Linux or OS X, you can give your node process priority using [nice](https://linux.die.net/man/1/nice) command. With `nice -n -20 COMMAND_YOU_USE_TO_RUN_NODE` it would have maximum priority (you may need to use `sudo`). – frangaren Sep 03 '20 at 16:28
-
1Most systems at most times have hundreds or thousands of threads in existence. Your CPU load might nevertheless be close to zero because the vast majority of them are sleeping, waiting for some work to do or some event to happen. Most task managers give you a way to adjust the priority of each process (a process consists of one or more threads). Usually it's fine to just let the OS do its thing though. – jmrk Sep 03 '20 at 16:54