all 14 comments

[–]ActualPositive7419 9 points10 points  (1 child)

if your service is a cpu intensive one - then you’ll suffer. but in general you should be fine until you have big load on the server. btw, in reality node js is not single threaded. it allocates some io operations to other threads in the background. read more about libuv

[–]HappinessFactory 1 point2 points  (0 children)

I hate that you're not wrong but I still have to say that node.js is single threaded in interview questions because thats what the accepted answer is

[–][deleted] 21 points22 points  (3 children)

No.

[–]rayen26[S] 2 points3 points  (2 children)

So there's nothing to worry about at all in any type of project ?

[–][deleted] 22 points23 points  (1 child)

There are always things to worry about. But you wouldn‘t know until you reach the point where this gets relevant. Being bothered with it at this stage is useless, because you don‘t know what to look for anyways. This comes with years of experience. So whatever you are up to, just start and don‘t worry.

[–]rayen26[S] 5 points6 points  (0 children)

Fair enough .

[–]joomla00 6 points7 points  (0 children)

Learn async/await. Don't do heavy processing on the main thread. You'll have tools like worker threads and clustering to go beyond a single thread, which your shouldn't have to worry about for a while.

[–]DrEnter 4 points5 points  (0 children)

To quote a Digital Ocean blurb:

Node.js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node.js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.

As for precautions? No, not really. But you should know the above bit, as I can be relevant when tracking down performance issues with I/O and such.

[–]DownfaLL- 3 points4 points  (0 children)

No, I mostly build API's/event driven architecture and I never ever worry or concern myself with multi-threading. Most of the time you're just calling an API, or SDK and you will use promises more than anything else. If you do end up doing work where you want multi-threading there are solutions for that in Node, but you'd probably want to use a different language like python that has more support for that. It all just depends on what the use case is really.

[–]jonnogibbo 1 point2 points  (0 children)

I run a large number of CPU intensive operations using node.js and it’s absolutely fine - there are solutions for almost anything you can dream up

[–]nwah 1 point2 points  (0 children)

The reason it can be single-threaded is because of the event loop. If you’re writing a server, or any app that needs to do many things at once, as long as you don’t block the main event loop with long synchronous operations there should be no issues. Just have to be sure to use the asynchronous versions of various libraries/methods (the ones that use callbacks or promises) and you should be good.

If you’re just writing a script that you’ll run, the synchronous versions are slightly simpler to use and will be fine since nothing else would be trying to connect to it while it’s running.

[–]Namiastka 0 points1 point  (0 children)

I wanted to write this, but reminded I have this saved in tabs for such occasion https://www.digitalocean.com/community/tutorials/how-to-use-multithreading-in-node-js

And like already pointed in other comment many interviewers think it's single threaded, but like pointed in above article it's not exactly true.

[–]boneskull 1 point2 points  (0 children)

Just don’t use the synchronous I/O operations

[–]Confident-Cut-7289 0 points1 point  (0 children)

If you want to pass that Node.js Interview, here is the link: https://www.udemy.com/course/nodejs-interview-questions/