Node.js 22 and 22.1: Stable WebSocket and On-Disk Code Caching

RisingStack's services:

Search

Node.js Experts

Learn more at risingstack.com

Sign up to our newsletter!

In this article:

Let’s explore the updates brought by Node.js 22, which promises enhancements in performance and development flexibility. Direct download links are available here.

Here’s a rundown of the key features and improvements in the latest release.

Stable WebSocket Support:

Node.js 22 now offers stable WebSocket support, which was previously experimental. This means we can use the built-in WebSocket client without depending on external libraries. This upgrade is significant for developers looking to establish real-time, bidirectional communication between clients and servers without incorporating third-party dependencies. 

Improved File Management with glob Functions:

Node.js 22 introduces new glob and globSync functions in the node:fs module, enhancing file management capabilities. These functions allow us to perform pattern matching for file paths, making it easier to locate and manipulate files based on specific criteria. By supporting pattern matching, we can efficiently handle file operations such as reading, writing, and deleting files that meet specific patterns or criteria. This improvement simplifies file management tasks and boosts the overall efficiency of Node.js applications.

V8 Engine Update to 12.4

Node.js 22 integrates V8 version 12.4, which introduces several cutting-edge features, including WebAssembly Garbage Collection and `Array.fromAsync`. This update also brings new Set methods and iterator helpers, expanding the toolkit for developers working with advanced data structures and asynchronous processes.

Maglev Compiler

A significant performance booster, the V8 Maglev Compiler, is now enabled by default on supported architectures. This is beneficial for short-lived CLI programs, making them run faster and more efficiently, demonstrating Node.js’s commitment to improving runtime performance.

Enhanced Module Support

This version marks a step towards seamless ECMAScript module (ESM) integration. With the `–experimental-require-module` flag, Node.js 22 allows the `require()` function to load ESM graphs synchronously. This means modules marked explicitly as ES with a `”type”: “module”` designation or `.mjs` extension not containing top-level await can now be loaded synchronously, bridging the gap between CommonJS and ESM formats.

Script Execution from `package.json`

Expanding its utility, Node.js 22 includes a new experimental feature that allows for the direct execution of scripts specified in `package.json` using the command line flag `node –run <script-in-package-json>`. This addition streamlines workflows by enabling script execution directly from the command line.

Stream Performance Improvements

To boost overall performance, the default High Water Mark for streams has been increased from 16KiB to 64KiB. This adjustment means a faster data handling capability at the cost of a slight increase in memory usage, but it can be customized for memory-sensitive environments.

What’s New in Node.js 22.1.0?

Node.js 22.1 introduces the `NODE_COMPILE_CACHE` feature, an enhancement to performance through on-disk code caching. By setting the `NODE_COMPILE_CACHE` environment variable to a directory path, Node.js automatically caches the compiled CommonJS and ECMAScript Module code on disk. The use of this feature can result in a slightly slower initial module loading but significantly speeds up subsequent loads if module content remains unchanged, exemplified by reducing the loading time of specific test fixtures from approximately 130ms to 80ms.

We can easily manage this cache by deleting the designated directory, which will be recreated upon future cache use. This functionality ensures compatibility across different Node.js versions by segregating caches by version in the same directory. However, it’s worth noting that using the code cache may impact the precision of JavaScript code coverage collected by V8, especially in functions deserialized from the cache. Disabling this feature during precise coverage testing is advised. This update builds upon the performance and functional advancements introduced in Node.js 22, further solidifying its utility in development environments.

Node.js continues to evolve, and with Node.js 22, it takes another leap forward in performance and functionality. As always, testing your applications with the new release is crucial for ensuring compatibility and taking full advantage of the new features. Remember, Node.js 18 will reach its End-of-Life in April 2025, so we advise you to plan your upgrade to Node.js 20 or the upcoming LTS version of Node.js 22.

Share this post

Twitter
Facebook
LinkedIn
Reddit

Node.js
Experts

Learn more at risingstack.com

Node.js Experts