Updates in Node.js 22

  • Node.js
  • JavaScript

Node.js 22 was released on April 24th, 2024. It will stay as the “Current” release for six months after which it will enter long-term-support(LTS).

V8 Update to 12.4

The V8 engine receives a significant upgrade to version 12.4, introducing key enhancements:

WebAssembly Garbage Collection

WasmGC is a new and promising way to implement GC languages in WebAssembly.

Array.fromAsync()

The Array.fromAsync() static method creates a new, shallow-copied Array instance from an async iterable, iterable, or array-like object. It awaits each value yielded from the object sequentially, while Promise.all() awaits all values concurrently.

Set methods

The Set object now provides powerful methods for composing sets using mathematical operations. These methods include:

  • A.difference(B): Returns the difference of sets A and B.
  • A.intersection(B): Returns the intersection of sets A and B.
  • A.symmetricDifference(B): Returns the symmetric difference of sets A and B.
  • A.union(B): Returns the union of sets A and B.
  • A.isDisjointFrom(B): Checks if sets A and B are disjoint.
  • A.isSubsetOf(B): Checks if set A is a subset of set B.
  • A.isSupersetOf(B): Checks if set A is a superset of set B.

Iterator Helpers

A collection of methods on the Iterator prototype that assist in using iterators effectively. These helpers are available on objects that have Iterator.prototype in their prototype chain.

  • .map(mapperFn)
  • .filter(filtererFn)
  • .take(limit)
  • .drop(limit)
  • .flatMap(mapperFn)
  • .reduce(reducer [, initialValue ])
  • .toArray()
  • .forEach(fn)
  • .some(fn)
  • .every(fn)
  • .find(fn)
  • Iterator.from(object)

Maglev Compiler: Boosting Performance

Node.js 22 enables the V8’s Maglev Compiler by default on supported architectures. The Maglev Compiler enhances performance for short-lived CLI programs.

Support for Synchronous ESM Graphs

This release introduces experimental support for requiring synchronous ECMAScript module (ESM) graphs using require(). With the --experimental-require-module flag, developers can load ESMs synchronously, facilitating more streamlined module handling in Node.js applications.

Running package.json Scripts

A new experimental feature in Node.js 22 allows for executing scripts defined in package.json directly from the command line using node --run <script-in-package-json>. This capability simplifies script execution and testing workflows within Node.js projects.

Stream Default High Water Mark

The default High Water Mark for streams has been increased from 16KiB to 64KiB in this release. This adjustment aims to enhance performance across various stream operations, optimizing memory utilization for better efficiency.

Stable Watch Mode (node —watch)

Watch Mode, which automatically restarts the Node.js process upon file changes, is now considered stable in Node.js 22. This feature facilitates real-time development by providing seamless file monitoring and process management.

WebSocket Implementation

The WebSocket implementation, previously experimental (--experimental-websocket), is now enabled by default. This integration empowers Node.js applications with a built-in WebSocket client, eliminating external dependencies for WebSocket functionality.

glob and globSync Functions

Node.js 22 introduces glob and globSync functions within the fs module, enabling developers to perform pattern-based file path matching. This addition simplifies file system operations, enhancing flexibility in handling file patterns.

Improved AbortSignal Performance

Enhancements in Node.js 22 optimize the creation of AbortSignal instances, resulting in improved performance for operations like fetch and the test runner. This refinement contributes to a smoother developer experience in asynchronous workflows.

Sources

  1. https://nodejs.org/en/blog/announcements/v22-release-announce
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync
  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator

KodingKorp — Join Our Monthly Mailing list to stay updated!

Originally published: https://www.kodingkorp.com/blogs/post/node.js-version-22