It's single threaded on the JavaScript side, but there are multiple threads under the hood of v8.

Answer from ralphtheninja on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 49137228 › v8-engine-is-single-threaded
node.js - V8 engine is single threaded? - Stack Overflow

It's single threaded on the JavaScript side, but there are multiple threads under the hood of v8.

Answer from ralphtheninja on stackoverflow.com
🌐
Quora
quora.com › Why-is-Node-js-single-threaded-Is-it-a-constraint-of-the-V8-engine-or-of-the-JavaScript-language-itself
Why is Node.js single threaded? Is it a constraint of the V8 engine ...
Answer (1 of 5): It is both a V8 constraint and sort of a JavaScript constraint, but mostly a consequence of Node’s design. The last reason is the most interesting I think. Yes, V8 is a single threaded execution engine. It’s built to run exactly one thread per JavaScript execution context.
🌐
Stack Overflow
stackoverflow.com › questions › 30638549 › why-node-js-is-fast-when-its-single-threaded
javascript - Why node.js is fast when it's single threaded? - Stack ...

First, why is a program faster when multi-threaded ?

It's partly due to the fact a multi-threaded program can run on multiple cores but the main reason, by far, is that when a thread is waiting for some IO operation (which is very often, especially in a server), the other threads can still progress.

Now, what about node ?

Node isn't single threaded. The user script in JS is executed in one thread but all IO operations are natively handled by libuv and the OS which are multi-threaded.

More explanation here.

In practice, this means that several requests are handled in parallel. Here's a very (very) simplified example of a possible sequence of actions:

user script                     | node + OS "threads" (libuv)
-------------------------------------------------------------
receive and analyze request 1   |
ask node for file 1             | fetching file 1
receive and analyze request 2   | fetching file 1
ask node for file 2             | fetching file 1, fetching file 2
prepare response header 1       | fetching file 2
tell node to send file 1        | send file 1, fetching file 2
prepare response header 2       | send file 1
tell node to send file 2        | send file 1, send file 2

The whole architecture of node (and io.js) makes it simple to have a high level of parallelism. The user thread is only called by the event loop for very short tasks which stop at the next IO operation (well, not really only IO, but most often) when your code gives to node a callback that will be called when the operation finished.

Of course this only works when you're using the asynchronous functions of Node. Any time you use a function ending in "Sync" like writeFileSync, you're defeating the concurrency.

Answer from Denys Séguret on stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 63727458 › when-we-say-that-the-javascript-engine-nodejs-is-single-threaded-does-that-me
node.js - When we say that the Javascript engine / nodejs is single ...

(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).

Answer from jmrk on stackoverflow.com
🌐
Medium
skchawala.medium.com › javascript-is-a-single-threaded-beast-then-how-the-heck-asynchronous-code-execution-works-bf3279bd7bff
Javascript is a single threaded beast, then how the heck asynchronous ...
October 12, 2024 - You might have heard or read somewhere that javascript is single threaded programming language unlike other multithreaded programming…
🌐
Groove Technology
groovetechnology.com › blog › why-javascript-is-single-threaded
Understanding Why JavaScript Is Single Threaded
September 26, 2024 - While this may seem like a limitation, there are several ways to work around its limitations using asynchronous programming and web workers. Understanding the limitations and working within them is critical to building fast and responsive web applications. A: Yes, JavaScript is always ...
🌐
GeeksforGeeks
geeksforgeeks.org › why-javascript-is-a-single-thread-language-that-can-be-non-blocking
Why JavaScript is a single-thread language that can be non-blocking ?
January 18, 2023 - Remember memory heap is not the ... gets executed line by line. Now, JavaScript is a single-threaded language, which means it has only one call stack that is used to execute the program....
🌐
Google
groups.google.com › g › v8-users › c › -efEIVq9a4g
v8 - Can v8 Engine Run different javascript in multi-thread in ...
That's JavaScript by design. JS has NO primitives to support threading at the script level, e.g. mutexes. Even if your engine allows multi-threaded access, your scripts cannot guaranty proper behaviour in the face of threads if those threads use any common data.
🌐
Stack Overflow
stackoverflow.com › questions › 21718774 › how-is-javascript-single-threaded
multithreading - How is Javascript single threaded? - Stack Overflow

JavaScript (in browsers) doesn't run concurrently2.

At most one of the setTimeout callbacks can execute at a time - as there is one JavaScript execution context or "thread".

However, the "next scheduled timeout" to run is always run .. next. The "4" runs before the "2" callback because it was scheduled to run sooner. The timeouts were effectively scheduled from the same time (none of the operations were blocking), but "2" had a much longer interval.

The underlying implementation may use threads1 - but JavaScript in the same global context doesn't run concurrently and guarantees consistent and atomic behavior between all callbacks.


1 Or it may not; this can be handled without any threads in a select/poll implementation.

2 In the same context: i.e. Tab/Window, WebWorker, host Browser Control. For example, while WebWorkers are run concurrently they do so in different contexts and follow the same asynchronous model (eg. as used by timers).

Answer from user2864740 on stackoverflow.com
Find elsewhere
🌐
DEV Community
dev.to › bbarbour › if-javascript-is-single-threaded-how-is-it-asynchronous-56gd
If Javascript Is Single Threaded, How Is It Asynchronous? - DEV ...
June 3, 2019 - So how do we get asynchronous code ... that, which has Web API that handle these tasks in the background. The call stack recognizes functions of the Web API and hands them off to be handled by the browser....
🌐
Stack Overflow
stackoverflow.com › questions › 26941911 › how-does-javascripts-single-threaded-model-handle-time-consuming-tasks
node.js - How does JavaScript's Single Threaded Model handle time ...

But if the callback function does infact take a long time to complete, won't JavaScript then be blocking everything else during that time as it is single threaded?

Yes.

How does nodejs handle such a problem?

Node.js handles nothing. How you handle concurrency is up to you and your application. Now, Node.js does have a few tools available to you. The first thing you have to understand is that Node.js is basically V8 (JavaScript engine) with a lightweight library split between JavaScript and native code bolted on. While your JavaScript code is single-threaded by nature, the native code can and does create threads to handle your work.

For example, when you ask Node.js to load a file from disk, your request is passed off to native code where a thread pool is used, and your data is loaded from disk. Once your request is made, your JavaScript code continues on. This is the meaning of "non-blocking" in the context of Node.js. Once that file on disk is loaded, the native code passes it off to the Node.js JavaScript library, which then executes your callback with the appropriate parameters. Your code continued to run while the background work was going on, but when your callback is dealing with that data, other JavaScript code is indeed blocked from running.

This architecture allows you to get much of the benefit of multithreaded code without having to actually write any multithreaded code, keeping your application straightforward.

I'm asking this question cause I have read that its generally good practice to keep function tasks as small as possible. Is it really because long tasks in javascript will actually block other tasks?

My philosophy is always to use what you need. It's true that if a request comes in to your application and you have a lot of JavaScript processing of data that is blocking, other requests will not be processed during this time. Remember though that if you are doing this sort of work, you are likely CPU bound anyway and doing double the work will cause both requests to take longer.

In practice, the majority of web applications are IO bound. They shuffle data from a database, reformat it, and send it out over the network. The part where they handle data is actually not all that time consuming when compared to the amount of time the application is simply waiting to hear back from the upstream data source. It is in these applications where Node.js really shines.

Finally, remember that you can always spawn child processes to better distribute the load. If your application is that rare application where you do 99% of your work load in CPU-bound JavaScript and you have a box with many CPUs and/or cores, split the load across several processes.

Answer from Brad on stackoverflow.com
🌐
Medium
mmomtchev.medium.com › in-2021-is-there-still-a-huge-performance-difference-between-javascript-and-c-for-cpu-bound-8ff798d999d6
In 2021, is there still a huge performance difference between ...
March 31, 2022 - I published very recently a new plugin for the JS bindings of GDAL that I currently maintain for importing and exporting N-dimensional arrays to scijs. scijs, while still far behind its Python…
🌐
Appsignal
blog.appsignal.com › 2020 › 07 › 01 › a-deep-dive-into-v8.html
A Deep Dive Into V8 | AppSignal Blog
July 1, 2020 - Learn the basics of V8's internal functioning, compilation and garbage collection processes, single-threaded nature, and more.
🌐
Stack Exchange
softwareengineering.stackexchange.com › questions › 350835 › feature-of-cpu-needed-to-run-javascript-fast
multithreading - Feature of CPU needed to run Javascript fast - ...

tl;dr- This is a complex, involved issue with a lot of variance. But for the sake of an answer here, CPU's with faster clock speeds are faster for JavaScript in most cases. Extra cores won't help much.

Very simplified answer

The full story's pretty involved and would be too much to fit into a short answer on StackExchange. Below, most of the statements are simplified, and shouldn't be taken as technically correct so much as to give a gist.

what is the feature of a CPU to run Javascript fast?

JavaScript programs are basically a series of instructions that the CPU executes in order. You want a CPU that executes those instructions fast. So, get a CPU with a fast clock rate.

I use to access the internet with an AMD Phenom II with 6 cores and I could almost have as much tabs open and almost everything is instantaneous. Then for the past 3 or so years, I've been accessing the internet with laptops and netbooks, which were Intel Celerons and Atoms and i just isn't like before.

Celeron's are cheap, low-end CPU's. Computers that contain them will generally be built from cheap, low-end parts. Atom's are meant to be small, portable, and low-power.

Neither will give you performance desktop results. That's not what they're for.

Supposing AMD Phenoms are comparable with Intel Cores (i3, i5 or i7s)

They're not. As far as I can find, they stopped making Phenom's quite a while ago, so they're old processors. AMD chips from 10 years ago aren't comparable to modern Intel i7's.

what exactly does make these more powerful CPUs faster with Javascript?

AMD Phenom's and Intel Core's use the same basic CPU architecture, x86-64, so we're able to compare them based on their metrics with some reasonable accuracy.

The big thing here is core speed, because JavaScript engines tend to be single-threaded which basically means that they only use one core. So if you've got a fancy million-core CPU, awesome, but it won't help you.

Variations in chip features (e.g. cache, pipelining, branch prediction) will cause further variation. Recently, Intel's been doing better than AMD with regards to this stuff, so Intel chips will tend to do better than comparable AMD chips with this sorta thing. Usually AMD helps to make up for this by offering more cores in their CPU's, which is great for other stuff, but irrelevant here.

I've always assumed them as high performance threading (Hyper-threading in the case of Intel) and multi-coring.

Nope. Lots of cores and Hyper-Threading-like features can be useful for multi-threaded workloads, but not most JavaScript stuff.

I've thought of branch prediction for a while, but I know that most of AMD's architectures aren't as aggressive in branch prediction compared to Intels.

Yup, that's a minor factor in Intel's favor. But, you can mostly ignore it for now since it's probably not as large as other factors.

But does SIMD (MMX, eMMX, etc) performance also count?

Depends on the JavaScript engine since that's what makes the choice between using those features or not. But, in general, I'd expect that, no, these aren't a major source of difference.

Does GPU performance matter?

Only if GPU acceleration's a bottleneck. I don't think that that's usually true for most JavaScript apps.

Answer from Nat on softwareengineering.stackexchange.com
🌐
Stack Overflow
stackoverflow.com › questions › 2734025 › is-javascript-guaranteed-to-be-single-threaded
concurrency - Is JavaScript guaranteed to be single-threaded? - ...

That's a good question. I'd love to say “yes”. I can't.

JavaScript is usually considered to have a single thread of execution visible to scripts(*), so that when your inline script, event listener or timeout is entered, you remain completely in control until you return from the end of your block or function.

(*: ignoring the question of whether browsers really implement their JS engines using one OS-thread, or whether other limited threads-of-execution are introduced by WebWorkers.)

However, in reality this isn't quite true, in sneaky nasty ways.

The most common case is immediate events. Browsers will fire these right away when your code does something to cause them:

var l= document.getElementById('log');
var i= document.getElementById('inp');
i.onblur= function() {
    l.value+= 'blur\n';
};
setTimeout(function() {
    l.value+= 'log in\n';
    l.focus();
    l.value+= 'log out\n';
}, 100);
i.focus();
<textarea id="log" rows="20" cols="40"></textarea>
<input id="inp">

Results in log in, blur, log out on all except IE. These events don't just fire because you called focus() directly, they could happen because you called alert(), or opened a pop-up window, or anything else that moves the focus.

This can also result in other events. For example add an i.onchange listener and type something in the input before the focus() call unfocuses it, and the log order is log in, change, blur, log out, except in Opera where it's log in, blur, log out, change and IE where it's (even less explicably) log in, change, log out, blur.

Similarly calling click() on an element that provides it calls the onclick handler immediately in all browsers (at least this is consistent!).

(I'm using the direct on... event handler properties here, but the same happens with addEventListener and attachEvent.)

There's also a bunch of circumstances in which events can fire whilst your code is threaded in, despite you having done nothing to provoke it. An example:

var l= document.getElementById('log');
document.getElementById('act').onclick= function() {
    l.value+= 'alert in\n';
    alert('alert!');
    l.value+= 'alert out\n';
};
window.onresize= function() {
    l.value+= 'resize\n';
};
<textarea id="log" rows="20" cols="40"></textarea>
<button id="act">alert</button>

Hit alert and you'll get a modal dialogue box. No more script executes until you dismiss that dialogue, yes? Nope. Resize the main window and you will get alert in, resize, alert out in the textarea.

You might think it's impossible to resize a window whilst a modal dialogue box is up, but not so: in Linux, you can resize the window as much as you like; on Windows it's not so easy, but you can do it by changing the screen resolution from a larger to a smaller one where the window doesn't fit, causing it to get resized.

You might think, well, it's only resize (and probably a few more like scroll) that can fire when the user doesn't have active interaction with the browser because script is threaded. And for single windows you might be right. But that all goes to pot as soon as you're doing cross-window scripting. For all browsers other than Safari, which blocks all windows/tabs/frames when any one of them is busy, you can interact with a document from the code of another document, running in a separate thread of execution and causing any related event handlers to fire.

Places where events that you can cause to be generated can be raised whilst script is still threaded:

  • when the modal popups (alert, confirm, prompt) are open, in all browsers but Opera;

  • during showModalDialog on browsers that support it;

  • the “A script on this page may be busy...” dialogue box, even if you choose to let the script continue to run, allows events like resize and blur to fire and be handled even whilst the script is in the middle of a busy-loop, except in Opera.

  • a while ago for me, in IE with the Sun Java Plugin, calling any method on an applet could allow events to fire and script to be re-entered. This was always a timing-sensitive bug, and it's possible Sun have fixed it since (I certainly hope so).

  • probably more. It's been a while since I tested this and browsers have gained complexity since.

In summary, JavaScript appears to most users, most of the time, to have a strict event-driven single thread of execution. In reality, it has no such thing. It is not clear how much of this is simply a bug and how much deliberate design, but if you're writing complex applications, especially cross-window/frame-scripting ones, there is every chance it could bite you — and in intermittent, hard-to-debug ways.

If the worst comes to the worst, you can solve concurrency problems by indirecting all event responses. When an event comes in, drop it in a queue and deal with the queue in order later, in a setInterval function. If you are writing a framework that you intend to be used by complex applications, doing this could be a good move. postMessage will also hopefully soothe the pain of cross-document scripting in the future.

Answer from bobince on stackoverflow.com
🌐
Medium
medium.com › sessionstack-blog › how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e
How JavaScript works: inside the V8 engine + 5 tips on how to write ...
July 11, 2023 - Couple of weeks ago we started a series aimed at digging deeper into JavaScript and how it actually works: we thought that by knowing the building blocks of JavaScript and how they come to play…
🌐
Reddit
reddit.com › r/node › why everyone is saying that js is single threaded? is that true?
r/node on Reddit: Why everyone is saying that JS is single threaded?

JS itself is single-threaded. This is a benefit, since constantly creating new threads can be relatively expensive.

Browsers and Node have added APIs to enable spawning new threads. These are meant for long-running, synchronous tasks that would otherwise block the main thread.

🌐
The NodeSource Blog
nodesource.com › blog › why-the-new-v8-is-so-damn-fast
Why the New V8 is so Damn Fast - NodeSource
July 24, 2018 - The entire V8 compiler pipeline was overhauled and shipped with Node.js version 8. This post investigates what speed improvements we can expect as a result.
🌐
Medium
medium.com › techtrument › multithreading-javascript-46156179cf9a
Multithreading Javascript. A Look Into Web Workers | by Max Peng ...
June 21, 2018 - As you may probably know, Javascript is single-threaded. To clarify better, this means that one single thread handles the event loop. For older browsers, the whole browser shared one single thread
🌐
Stack Overflow
stackoverflow.com › questions › 5168718 › what-blocks-ruby-python-to-get-javascript-v8-speed
What blocks Ruby, Python to get Javascript V8 speed? - Stack Overflow

What blocks Ruby, Python to get Javascript V8 speed?

Nothing.

Well, okay: money. (And time, people, resources, but if you have money, you can buy those.)

V8 has a team of brilliant, highly-specialized, highly-experienced (and thus highly-paid) engineers working on it, that have decades of experience (I'm talking individually – collectively it's more like centuries) in creating high-performance execution engines for dynamic OO languages. They are basically the same people who also created the Sun HotSpot JVM (among many others).

Lars Bak, the lead developer, has been literally working on VMs for 25 years (and all of those VMs have lead up to V8), which is basically his entire (professional) life. Some of the people writing Ruby VMs aren't even 25 years old.

Are there any Ruby / Python features that are blocking implementation of optimizations (e.g. inline caching) V8 engine has?

Given that at least IronRuby, JRuby, MagLev, MacRuby and Rubinius have either monomorphic (IronRuby) or polymorphic inline caching, the answer is obviously no.

Modern Ruby implementations already do a great deal of optimizations. For example, for certain operations, Rubinius's Hash class is faster than YARV's. Now, this doesn't sound terribly exciting until you realize that Rubinius's Hash class is implemented in 100% pure Ruby, while YARV's is implemented in 100% hand-optimized C.

So, at least in some cases, Rubinius can generate better code than GCC!

Or this is rather matter of resources put into the V8 project by Google.

Yes. Not just Google. The lineage of V8's source code is 25 years old now. The people who are working on V8 also created the Self VM (to this day one of the fastest dynamic OO language execution engines ever created), the Animorphic Smalltalk VM (to this day one of the fastest Smalltalk execution engines ever created), the HotSpot JVM (the fastest JVM ever created, probably the fastest VM period) and OOVM (one of the most efficient Smalltalk VMs ever created).

In fact, Lars Bak, the lead developer of V8, worked on every single one of those, plus a few others.

Answer from Jörg W Mittag on stackoverflow.com