In this article we are going to dive into the updates of the latest Node release.
Node.js 13 was released 2020/02/05, and we need to keep in mind that it will not be in Long Term Support (LTS), so using it in production environments is not recommendable. Node.js 12 was promoted to (LTS).
These releases deliver faster startup and better default heap limits, updates to V8, TLS, llhttp, new features including diagnostic report, bundled heap dump capability and updates to Worker Threads, N-API, and more.
It brings performance tweaks and improvements:
It’s a library which provides Unicode and Globalization support for software applications. It was already supported on previous versions, the thing is that in this version it supports a larger set of languages by default.
Locales string can be found here:
https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
Experimental support for ECMAScript modules is enabled by default, and are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.
.mjs is the extension for these types of modules, they can't be loaded using require. If you are using commonJS use import() and it will return a promise.
To use them in the package.json we have to add:
Module (hi.mjs):
Usage example:
Usage on commonJS:
It's an API that provides access to several operating-system-like features, including files and filesystems, Berkeley sockets, clocks, and random numbers, that we'll be proposing for standardization.
It's designed to be independent from browsers, so it doesn't depend on Web APIs or JS, and isn't limited by the need to be compatible with JS. And it has integrated capability-based security, so it extends WebAssembly's characteristic sandboxing to include I/O.
The --experimental-wasi-unstable-preview1 and --experimental-wasm-bigint CLI arguments are needed for it to work.
Node.js is still event looping, but now we have the chance to multithread.
In this example we are going to sum numbers from a big range so it will take a lot of time to do on a single thread and we can see how we can improve it to make it faster.
Using one worker (almost normal Node.js code):
Using three workers:
Worker: class that represents an independent JavaScript execution thread. Most Node.js APIs are available inside of it.
isMainThread: function that only returns true if the code is is not running inside of a Worker thread
parentPort: If this thread was spawned as a Worker, this will be a MessagePort allowing communication with the parent thread. Messages sent using parentPort.postMessage() will be available in the parent thread using worker.on('message'), and messages sent from the parent thread using worker.postMessage() will be available in this thread using parentPort.on('message').
workerData: an arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor.
So as you can see, if we have to process a large amount of data or need to use big calculation power, it will be greatly improved using workers. But it doesn't mean that as more workers are used it will be done faster. Creating workers has its cost so we need to find a balance.
Ready to get started? Use the form or give us a call to meet our team and discuss your project and business goals.
We can’t wait to meet you!
Call us!
+1 (347) 871 09 22
Write to us!
info@vairix.com