Tuesday 20 September 2022

What’s New in Node.js 18: A Comprehensive Guide from iFour Technolab

 

What’s new in Node.js 18: A comprehensive guide

Content Source: What’s New in Node.js 18: A Comprehensive Guide from iFour Technolab 


Node.js 18 has arrived! What does it bring to the table for Node.js development companies and their developers?

Applications built using Node.js are extremely quick and scalable. But what makes it so quick? Obviously, its support for asynchronous and single threaded IO model. Node.js releases a new version every six months, bringing new features to the LTS (Long Term Support) channel. The most recent version of Node.js have been phenomenal as it brought remarkable and improved capabilities to the board. It included updates to the V8 engine to 10.1, a core test runner module, and global fetch enabled by default.

It will be supported for 6 months before being switched to LTS, where it will be referred with a codename as the "Hydrogen".

Node 18 is now available! What changes does it bring?

Node.js 18 was released on April 19, 2022, with incredible feature support. Before we get into the details, here is an overview of the features we will be discussing in this blog.

  • Browser-compatible APIs can be accessed globally
  • Fetch and Built-in Test Runner Module
  • Web Streams API
  • Build Time user-land Snapshot
  • Compiler and Toolchain Upgrades
  • V8 10.1
  • Other updates of Node.js

Looking to hire Node.js developers? Your search ends here.

Browser-compatible APIs can be accessed globally

Previously, experts had to rely on third-party packages (such as Axios) and the http.request API, which needed a large amount of code to call and get data. Also, the Fetch API was experimental at this point and will remain tentative until further test coverage is added. Moreover, the aspects that previously needed a browser were now removed in this version.

An example of this API is as follows:

<xmp>
const response = await fetch(&#39;https://nodejs.org/api/documentation.json&#39;);
if (response.ok) {
const data = await response.json();
console.log(data);
}
</xmp>

Using this, the following globals are made available: Request, fetch, FormData, Headers, and Response. You can disable the API by using the command --no-experimental-fetch.

Test Runner Module

Node professionals may now develop project tests without having to rely on third-party packages or libraries. This is one of the most important enhancements included in this edition.

It allows experts to write JavaScript tests and present the results in TAP format. When a disc image is generated from a data storage tape prepared for ZX spectrum systems or C64 computers, it is referred to as TAP format. To go there, type:

Import test from ‘Node:test’;

Here is an example of it:

<xmp>
test(&#39;top level test&#39;, async (d) => {
await d.test(&#39;subtest 1&#39;, () => {
assert.strictEqual(1, 1);
});
await d.test(&#39;subtest 2&#39;, () => {
assert.strictEqual(2, 2);
});
});
</xmp>

The test runner module is only available when using the node: prefix. Note that the node: prefix indicates the loading of a core module. If the prefix is left out, importing ‘test’ would attempt to load a userland module.

Web Streams API

Node.js has now unveiled the experimental implementation of the Web Stream APIs. This statement basically means that the following APIs are available globally:

  • ReadableStream, ReadableStreamDefaultReader, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableByteStreamController, TransformStream, ReadableStreamDefaultController, TransformStreamDefaultController, WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController, ByteLengthQueuingStrategy, CountQueuingStrategy, TextEncoderStream, TextDecoderStream, DecompressionStream, CompressionStream.
Web-stream-API-Nodejs

The above diagram helps you understand how web stream APIs are employed in Node.js.

Build Time user-land Snapshot

Node.js 18 may now allow users to build a Node.js binary with a custom V8 start-up snapshot using the --node-snapshot-main flag of the configure script. Here is a brief look:

<xmp>
$ cd /path/to/node/source
$ ./configure --node-snapshot-main=marked.js
# Build the binary
$ make node
</xmp>

The resultant binary can deserialize the state of the heap which was initialized by the snapshot entry point during the build. Thus, the application in generated binary can be initialized quicker using the following steps:

<xmp>
// render.js
// snapshot so there is will be no need to parse & execute the
// module again, which improves the start-up time.
const HTML = globalThis.marked(process.argv[1]);
console.log(HTML);
</xmp>

We can execute the generated binary like this:

<xmp>
$ out/Release/node render.js test.md
</xmp>

To improve this, the Node.js team is working on JavaScript APIs to minimize additional runtime and make it into a single-file executable program. They are also attempting to build snapshots that can be generated and loaded without the use of a compiler.

Compiler and Toolchain Upgrades

Node.js brings forth pre-built binaries for several different platforms. For every major release, the minimum toolchains are assessed and updated cautiously.

  • The pre-built libraries are built on Red Hat Enterprise Linux (RHEL) 8 and are compatible with Linux distributions that are based upon glibc 2.28 or later, for example, Ubuntu 20.04, Debian 10, RHEL 8, etc.
  • The minimum architectural support for AIX has been upgraded from Power 7 to Power 8.
  • MacOS 10.15 or later is essential for prebuilt binaries of Mac Operating System.

Earlier, the prebuilt binaries were not accessible on Windows 32-bit due to difficulties encountered while generating V8 dependencies. Several experts assumed that the team would restore 32-bit Windows binaries in this new release. However, Node has discontinued support for Operating Systems that are no longer supported by their respective suppliers.

Seeking for a reliable custom software development company? Contact us now.

V8 10.1

An interesting aspect of this new version is the V8 engine update. It was updated to version 10.1 which is being a part of Chromium 101. Compared to the version included in Node.js 17.9.0, the following features are included:

  • Improvements to the Intl.Locale API
  • The findLastIndex() and findLast() array methods.
  • The Intl.supportedValuesOf function.
  • Improved performance of class fields and private class methods (Their initialization is now as fast as ordinary property stores).

Miscellaneous updates of Node.js

The Node community is constantly working on ECMAScript modules, loaders and their enhancements. As part of their goal-oriented milestone, they are also striving to introduce experimental support for JSON Import Assertions, unflagging of JSON modules and experimental support for HTTPS and HTTP import as well.

The team is also working on the 'Next 10,' which relates to Node's reflection on the previous ten years and Node's future vision for the upcoming ten years.

Conclusion

Node.js has been the preferred option for many major firms like Netflix, IBM, LinkedIn, Walmart, Paypal, GoDaddy, and many more, due to its ultrafast capabilities and perfect features. Node.js community always strives to improve user experience. Given this, they've made several intriguing modifications that will help developers build more efficient apps. In this blog, we have gone through the extensive updates introduced in the latest release i.e., Node.js 18.

No comments:

Post a Comment

Unleash the Power of Explainable AI in Banking Software Development

  What exactly is Explainable AI? Explainable AI (XAI) is a subfield of Artificial intelligence (AI) that focuses on developing algorithms a...