What’s New In Node.js 16?
Node.js has launched its latest major version. Released in April 2021, v16 is the new “current” release. In October 2021, it will be promoted to the Long Term Support (LTS) release with maintenance continuing into 2024.
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. Although already available in v15, the changes are new to LTS users.
JavaScript Engine Updates
Node.js 16 ships with v9.0 of the V8 JavaScript engine. This replaces V8 8.6 in Node.js 15. In the months between the two releases, V8 has landed several new features alongside significant performance enhancements.
One such improvement is faster calls to functions with a mismatched argument list length. JavaScript permits you to pass a different number of arguments to the function’s signature:
function demo(a, b, c) { // ... } // OK demo(1); // Also OK demo(1, 2, 3, 4);
Advertisement
The first variant is commonly used when a parameter is optional. The latter variant sometimes occurs when a function is refactored to remove an argument. This kind of code incurred a performance penalty prior to V8 8.7. Reworking the internals used to handle mismatched argument list lengths has virtually eliminated the overhead.
Another performance improvement concerns the super
property. This JavaScript feature lets you access the parent of a class that’s using inheritance. Calls to super
used to be unoptimised, incurring a runtime call on each use. V8 v9 shipped changes enabling super
property access to execute orders of magnitude more quickly. This optimisation is now available to Node.js users.
Finally, V8 v9 comes with a significant new feature for regular expressions. You can now request an array containing the start and end positions of each matched capture group. This is enabled using the /d
flag with RegExp
. You can then access the indices array via the indices
property of the execution result object.
const regex = /(a)(b)/d; const match = regex.exec("xy"); console.log(match.indices[0]); // [0, 2] console.log(match.indices[1]); // [0, 1] console.log(match.indices[2]); // [1, 2]
The first item in the array represents the entire match. The subsequent elements give you the positions of each matched capture group in the input string.
npm v7
Node.js v16 will introduce npm v7 to the LTS channel. npm underwent a substantial internal refactoring to improve performance and overhaul the peer dependencies workflow.Advertisement
Peer dependencies are now installed automatically alongside regular dependencies. This might be a breaking change in some workflows, although disruption’s “usually minimal.”
There’s also a new format for package-lock.json
which enables more reliable deterministic builds. It now includes everything npm needs to rebuild your package tree from scratch. If you’re migrating from Yarn, npm’s gained support for yarn.lock
files too.
Other Notable Changes
v16 promotes the Timers Promises API to stable status. This API provides JavaScript timer functions that return native promises. Functions such as setTimeout()
usually accept a callback. This can be unwieldy when used with modern asynchronous code.
// Old function demoOld() { setTimeout(() => { console.log("Timer expired."); }, 5000); } // New import {setTimeout} from "timers/promises"; async function demoNew() { await setTimeout(5000); console.log("Timer expired."); }
Using promises makes for more readable code when combined with async
/await
. Waiting for a timer feels like a traditional sleep
call in a synchronous language.
Node.js has also added experimental support for the Web Crypto API. This W3C specification is intended to give web applications access to an elementary set of cryptographic functions. The API offers hash generation and signature verification alongside encryption and decryption utilities.Advertisement
Offering Web Crypto support in Node.js improves interoperability between JavaScript on servers and JavaScript in web browsers. The Node implementation extends various aspects of the W3C spec and includes its own variations of some objects.
Apple Silicon
A change that’s completely new in v16 is the publication of builds for Apple Silicon processors. This should improve performance on new Mac hardware. Node.js will run natively on the Apple M1, instead of using the Rosetta emulation layer.
Users installing via the tarball distribution mechanism will need to choose between darwin-x64
(Intel) or darwin-arm64
(Apple Silicon) packages. If you use the macOS install utility, a single download with support for both architectures is provided.
Deprecations
Being a major release, v16 comes with some deprecations and removals. You can find the full list in the complete changelog.
A notable deprecation is the intent to remove access to several core modules via process.binding()
. This call provides a mechanism to access the underlying C++ objects behind the Javacript implementation of modules. Bindings for modules including crypto
, v8
, async_wrap
and url
have been deprecated and could be removed in the future.
Elsewhere, the recursive
option to the fs.rmdir()
directory removal function has been deprecated. New code should use the alternative fs.rm(path, {recursive: true})
function instead. This change has been made to better align Node.js with the rmdir
Unix command and rmdir()
-equivalent functions in other programming languages.
Summary
Node.js v16 is a major new release which will bring several new features to the LTS channel while rolling the latest V8 JavaScript features. This comes alongside npm v7 and the launch of Apple Silicon-native builds.
You can get the new release via the current channel by following the installation docs on the Node.js website. v16 will replace v14 as the LTS release on October 26th, 2021.
The current Node.js v15 release will remain supported until June 1st, 2021. Users should look to upgrade to v16 as soon as possible. The currently active LTS branch, v14, will be maintained through the end of April 2023.
Source: cloudsavvyit