Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Node.js Cookbook

You're reading from   Node.js Cookbook Practical recipes for building server-side web applications with Node.js 22

Arrow left icon
Product type Paperback
Published in Nov 2024
Publisher Packt
ISBN-13 9781804619810
Length 456 pages
Edition 5th Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Bethany Griggs Bethany Griggs
Author Profile Icon Bethany Griggs
Bethany Griggs
Manuel Spigolon Manuel Spigolon
Author Profile Icon Manuel Spigolon
Manuel Spigolon
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: Introducing Node.js 22 2. Chapter 2: Interacting with the File System FREE CHAPTER 3. Chapter 3: Working with Streams 4. Chapter 4: Using Web Protocols 5. Chapter 5: Developing Node.js Modules 6. Chapter 6: Working with Fastify – The Web Framework 7. Chapter 7: Persisting to Databases 8. Chapter 8: Testing with Node.js 9. Chapter 9: Dealing with Security 10. Chapter 10: Optimizing Performance 11. Chapter 11: Deploying Node.js Microservices 12. Chapter 12: Debugging Node.js 13. Index 14. Other Books You May Enjoy

Adopting new JavaScript syntax in Node.js 22

The formal specification for the JavaScript language is ECMAScript. New JavaScript features make their way into Node.js via updates to the underlying V8 JavaScript engine that the Node.js runtime is built on top of. ECMAScript has annual updates that provide new JavaScript language features and syntax.

New major versions of Node.js tend to include a significant upgrade to the V8 engine. Node.js version 22.0.0 was released with V8 version 12.4. However, the V8 version may be upgraded during the lifetime of Node.js 22.

Updated versions of V8 bring underlying performance improvements and new JavaScript language features and syntax to the Node.js runtime. This recipe will showcase a couple of the newer JavaScript language features that have been introduced in Node.js 22.

Getting ready

For this recipe, you will need to have Node.js 22 installed. You will also need to have access to a terminal.

How to do it…

In this recipe, we will be using the Node.js Read Eval Print Loop (REPL) to test out the newer JavaScript features that are available in Node.js 22. Follow these steps:

  1. First, let’s open the Node.js REPL. Enter the following command in your terminal:
    $ node
  2. This should open the REPL, which is an interface that we can use to execute code. Expect to see the following output:
Figure 1.7 – Node.js REPL

Figure 1.7 – Node.js REPL

  1. Start by entering the following command. This command will return the version of V8 that is embedded in the Node.js version you’re using:
    > process.versions.v8
    '12.4.254.21-node.19'
  2. Two new JavaScript String methods were made available since Node.js 20:
    • String.prototype.isWellFormed: This method returns whether the supplied String is well-formed UTF-16
    • String.prototype.toWellFormed: This method will replace unpaired surrogates with the replacement character (U+FFFD), thus making the UTF-16 String well-formed.

    You can demonstrate this in the REPL:

    > "006E006F00640065".isWellFormed()
    true
  3. Another recent feature addition is the Intl.NumberFormat built-in object, which provides language-based number formatting. Let’s test this out. In the REPL, declare a number:
    > const number = 123456.789;
    undefined
  4. Next, let’s format that number as if it were the Great British Pound (GBP):
    > new Intl.NumberFormat('en-UK', { style: 'currency', currency: 'GBP' }).format(number);
    '£123,456.79'
  5. In Node.js 22, new Set methods like union, intersection, and difference were added as part of the V8 12.4 update. These enhancements make it easier to perform operations on numeric sets. Here’s an example involving prime and odd numbers:
    > const oddNumbers = new Set([1, 3, 5, 7]), primeNumbers = new Set([2, 3, 5, 7]);
    undefined
    > console.log('All Numbers:', [...(oddNumbers.union(primeNumbers))].toString());
    All Numbers: 1,3,5,7,2
    undefined
    > console.log('Common Numbers:', [...(oddNumbers.intersection(primeNumbers))].toString());
    Common Numbers: 3,5,7
    undefined
    > console.log('Exclusive Primes:', [...(primeNumbers.difference(oddNumbers))].toString());
    Exclusive Primes: 2

Using the REPL, we’ve explored a couple of the new JavaScript language features that are available in Node.js 22. The main goal of this learning is that new JavaScript language features become available through upgrading the underlying Google Chrome V8 engine.

How it works…

New JavaScript language features are introduced into Node.js via updates to the underlying Google Chrome V8 JavaScript engine. A JavaScript engine parses and executes JavaScript code. The embedding of the Google Chrome V8 engine in Node.js is what enabled the execution of JavaScript outside of the browser. Chrome’s V8 JavaScript engine is one of many available JavaScript engines, with Mozilla’s SpiderMonkey, which is used in the Mozilla Firefox browser, being another leading JavaScript engine.

Every 6 weeks, a new version of Google Chrome’s V8 engine is released. Node.js 22 will continue to incorporate updates into V8, provided they can be made application binary interface (ABI)-compatible. An ABI describes how programs can interact with functions and data structures via compiled programs. It can be considered similar to a compiled version of an application programming interface (API).

Once there is a release of V8 that no longer allows ABI compatibility, the specific release line of Node.js will be fixed on that version of V8. However, specific V8 patches and fixes may continue to be applied directly to that Node.js release line. Node.js 20 is now fixed on V8 version 11.3, whereas Node.js 22, at the time of writing, is at V8 12.4. The V8 version in Node.js 22 will continue to be updated until ABI compatibility of newer versions of V8 can no longer be maintained.

The V8 JavaScript engine compiles JavaScript internally using just-in-time (JIT) compilation. JIT compilation speeds up the execution of JavaScript. While V8 is executing JavaScript, it obtains data about the code that is being executed. From this data, the V8 engine can make speculative optimizations. Speculative optimizations anticipate the upcoming code based on the code that has recently been executed. This allows the V8 engine to optimize for the upcoming code.

The V8 blog provides announcements of new V8 releases and details the new features and updates to V8. The V8 blog can be accessed at https://v8.dev/blog.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image