Javascript is famous for being a single-threaded language, but there are ways to work around that today
Javascript is a wonderful programming language, but one of the main issues that we had on Javascript for a long time has been that it’s single threaded.
On the frontend, we have access to the web worker API, a standard API which allows some multi-threading on the browser side.
On the backend side, it’s another story. With version 10.X arrived a new module called worker thread
as an experimental feature and version 12.X made it stable.
Keep in mind that asynchronous functions are not threads in JS. This is a common mistake.
The NodeJS team made it pretty straightforward. You are going to need an entry file written is ES5 Javascript (ES6+ is not supported and you cannot use Babel/Typescript on files used directly or indirectly by your worker thread)
You’ll have to import the attribute Worker
from the worker_threads
module and instantiate it. Use the absolute path from the package.json location (you cannot use relative path here) to the entry file.
#nodejs #multithreading #javascript #parallel-computing