Node.js 21.0.0 was released!

Node.js 21 was released on October 17, 2023, and includes a number of new features and improvements. Here is a story about some of the new features in Node.js 21, with examples:

The new built-in WebSocket client

Once upon a time, there was a Node.js developer named Alice who needed to build a real-time chat application. She had used third-party WebSocket libraries in the past, but she wanted to try using the new built-in WebSocket client in Node.js 21.

Alice started by creating a new Node.js project and installing Node.js 21. Then, she created a new file called server.js and added the following code:

const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 8080 });

server.on('connection', (socket) => {
  console.log('New client connected!');

  // Handle incoming messages from the client
  socket.on('message', (message) => {
    console.log('Received message:', message);

    // Broadcast the message to all connected clients
    server.clients.forEach((client) => {
      client.send(message);
    });
  });
});

Alice then started the server with the following command:

node server.js

Now, Alice could open a web browser and navigate to http://localhost:8080 to connect to the chat application. She could then type a message and press Enter to send it to all other connected clients.

The experimental --experimental-default-type flag

Once upon a time, there was a Node.js developer named Bob who was working on a large codebase. He was frustrated with the fact that he had to manually specify the module type (CommonJS or ESM) for every new file he created.

Bob heard about the new experimental --experimental-default-type flag in Node.js 21. This flag allows you to specify the default module type for all new files in your project.

Bob added the following flag to his nodemon command:

nodemon --experimental-default-type=esm server.js

Now, when Bob created a new file in his project, it would automatically be treated as an ESM module. This saved Bob a lot of time and hassle.

Improved performance and new language features in the V8 JavaScript engine

Node.js 21 includes an updated version of the V8 JavaScript engine, which brings improved performance and new language features.

One of the new language features is Array grouping. This allows you to group elements in an array based on a common property. For example, the following code groups the elements in an array of users by their country:

const users = [
  { name: 'Alice', country: 'USA' },
  { name: 'Bob', country: 'UK' },
  { name: 'Carol', country: 'Canada' },
];

const groupedUsers = Array.groupBy(users, (user) => user.country);

console.log(groupedUsers);

This would print the following output to the console:

{
  USA: [{ name: 'Alice', country: 'USA' }],
  UK: [{ name: 'Bob', country: 'UK' }],
  Canada: [{ name: 'Carol', country: 'Canada' }],
}

Another new language feature is ArrayBuffer.prototype.transfer. This allows you to transfer ownership of an ArrayBuffer to another process. This can be useful for improving performance when working with large datasets.

For example, the following code transfers ownership of an ArrayBuffer to a worker thread:

const arrayBuffer = new ArrayBuffer(1024 * 1024);

const worker = new Worker('worker.js');
worker.postMessage(arrayBuffer, [arrayBuffer]);

worker.onmessage = (event) => {
  // The worker thread has finished processing the ArrayBuffer
};

The worker thread could then process the ArrayBuffer without having to copy it to a new location.

These are just a few of the new features in Node.js 21.