What’s New In Node.js 16?

What's New In Node.js 16

Recently, Node.js v16 is released and this version will soon be moved to LTS status. The LTS status assures that the critical bugs are solved within 30 months. The Node.js release process sees updates from the “current” branch merged into the next major feature release. Consequently, v16 brings several recently added v15 features into what will eventually become the LTS branch. Here we’ll see the new features and upgrades of Node.js v16.

New Features In Node.js 16-

1. Updated Platform Support-

With the major releases, this release updates the minimum supported levels for platforms and tooling that are used to build Node.js. Some of the instances include updating the minimum supported Xcode version to 11 and the GCC version for Linux and AIX platforms to 8.3. More fascinating is the work being done to support for the new Apple M1 architecture. Team is active in the Node.js build working group, helping to keep the infrastructure running to support the Power PC and S390 architectures, and helps with work across the other architectures.

2. V8 JavaScript Engine Version 9-

Latest version Node.js 16 introduces version 9 of V8 Javascript Engine. This new version takes the place of V8 8.6 in Node.js 15. Previously, V8 has introduced lots of new and unique features with excellent performance improvements between two consecutive releases. 

V8 Javascript engine is a runtime environment to implement Javascript code. Usually, as the V8 JavaScript engine allows JavaScript to run across several platforms, developers are not required to worry about the working and functioning of their code running on Windows, Linux or MacOS. In any case, V8 needs the Node team to deal with and monitor and optimize the V8 for different hardware combinations and operating systems written in C++. Another amazing performance upgrade includes the super property. 

The Javascript feature lets you to access the class’s parent that uses its inheritance. Great property of V8 v9 changes lets users to execute orders rapidly. Now this optimization is available to all Node.js users. With V8 version 9, you get unique features for regular expressions. With it, you can request a set comprising the start and endpoints of each matched capture group. One can enable it by using flag /d in regular expressions. You can use indices array through the indices property of result object.

3. npm v7-

Node.js v16 will bring npm v7 to the LTS channel. npm went through a significant internal refactoring for performance improvement and redesign the peer dependencies workflow. Now, peer dependencies are installed automatically with regular dependencies. This may be a breaking change in certain workflows, in spite of the fact that disruption’s “normally negligible.” Also there’s a new format for package-lock.json that enables more reliable deterministic builds. Now, it includes all npm needs to rebuild your package tree from scratch. If you’re migrating from Yarn, npm’s support for yarn.lock files also.

4. N-API Version 8-

Node.js 16 is continuously stepping ahead to make it easy to build, create and support native modules referred to as add-ons. Node.js 16 brings NODE-API v8 supports- 

  • Freezing objects like-napi_object_freeze
  • Sealing objects like- napi_object_seal
  • Type tagging objects like- napi_type_tag_object/napi_check_object_type_tag

Add ons in each version of NODE-API are generated by the use cases introduced to the team by real-world use.

5. Apple Silicon-

Publication of builds for Apple Silicon processors is completely new in v16. This should improve performance on new Mac hardware. Node.js will run natively on Apple M1, rather than using Rosetta emulation layer. Users install it via tarball distribution mechanism will need to choose between darwin-x64 (Intel) or darwin-arm64 (Apple Silicon) packages. If you use macOS install utility, just a single download with support for both architectures is provided.

6. Deprecations-

With major release, v16 comes with some deprecations and removals. A notable deprecation is the aim to remove access to a few core modules by means of process.binding(). This call provides mechanism to access underlying C++ objects behind Javascript implementation of modules. Bindings of modules include crypto, v8, async_wrap and url have been deprecated and could be removed in future. Somewhere else, the recursive option to the fs.rmdir() directory removal function has been deprecated. New code should use the option fs.rm(path, {recursive: true}) function instead. This change has been made to more readily adjust Node.js to the rmdir Unix order and rmdir()– identical capacities in other programming dialects. This change has been made to better align Node.js with rmdir Unix command and rmdir()-equivalent functions in other programming languages.

7. The Promise APIs-

Strategic step is taking place inside the project to affix promise-based APIs. One such addition to this release includes the addition of Promise-based time APIs. Now you can do following-

Input-

import {setTimeout} from ‘timers/promises’;
async function waitForTimer() {
let curTime = new Date();
console.log(‘timer started:’ + curTime);
await setTimeout(2000);
curTime = new Date();
console.log(‘timer expired:’ + curTime);
}
waitForTimer();

Output-

timer started:Fri Apr 09 2021 13:44:33 GMT-0400
timer expired:Fri Apr 09 2021 13:44:35 GMT-0400

Basically, v18 advertises the timers’ promises API. This API offers Javascript timer functions which give back native promises. Functions such as setTimeout() accept a callback. This can be unmanageable when you use it with the latest asynchronous code. Also, when promises are combined with await/async, this makes code more readable. Awaiting for timer is same as a conventional sleep call in a synchronous language. Also, Node.js introduced exclusive experimental support and assistance for Web Crypto API. This specific W3C specification aims to provide web app access to a simple array of Cryptographic functions. API provides signature verification with hash generating apart from encryption and decryption features. By allowing the web crypto support with Node.js improves the interoperability among Javascript present in Web browser and server. Implementing node expands some aspects of W2C specification. Also it comes with its own array of variations of certain objects.

8. Async Local Storage APIs-

One of the main areas to focus on helping customers in identifying and managing the issues in production includes problem determination and observability. Also, the concept of AsyncLocalStorage API becomes easy, reliable and rapid to turn stable as compared to the fundamental Async Hooks. AyncLocalStorage API stable is still not found for 16 release, it is noticeable that Node.js collaborators are working blood and sweat on this. Major step is to offer an inbuilt set of APIs used by some packages like OpenTelemetry to help the tracing component of Observability in the apps of Node.js.  Let us see the codes using the AsyncLocalStorage to see the advantages that it brings with it.

Input-

const AyncLocalStorage = require(‘async_hooks’).AsyncLocalStorage;
const asyncLocal1 = new AsyncLocalStorage();
let sharedFlowId = “”;
function asyncflow(flowide) {
setTimeout( () => {
asyncLocal1.run(new object(), () => {
sharedFlowId = flowid;
asyncLocal1.getStore()[‘flowid’] = flowid;
setTimeout => {
console.log(flowid + ‘:’ + sharedFlowId ‘:’ + asyncLocal1.getStore()[‘flowid’]);
}, Math.random()*500);
}));
}, Math.random()*500);
}
asyncflow(‘flow1’);
asyncflow(‘flow2’);
asyncflow(‘flow3’);

Output-

flow2:flow2:flow2
flow1:flow1:flow1
flow3:flow3:flow3

flow2:flow2:flow2
flow1:flow2:flow1
flow3:flow2:flow3

flow3:flow3:flow3
flow1:flow2:flow1
flow2:flow2:flow2

For some executions, the output follows:

closure:shared:asLocal

Know the amazing Node.js IDEs For App Development at- Top 11 Node.js IDEs For App Development

Wrap Up-

Node.js v16 is a major new release that bring several new features to the LTS channel while rolling the latest V8 Javascript features. If you are still stuck with the old version, know the new version features and enter the new version. There can be some other features with new version. If you’re thinking to use Node.js for your next project, consult with Solace experts. We are here to help you through consultation and development. You can also hire Node.js developers of Solace team for effective software development with Node.js. Connect with Solace and get a free quote for Node.js development. We will be happy to help you.

Related Post