Node.js 10 State of the Web
Apps

Node.js 10: Should you Upgrade? [State of the Web]

Lesezeit
6 ​​min

Notice:
This post is older than 5 years – the content might be outdated.

The recently released Node.js 10 is the newest version of the popular server-side JavaScript runtime. As with each even numbered Node.js release, this version will have long-term support and receive updates till April 2021. For applications still running on Node 6 or even Node 4, this is exactly the right time to upgrade.

Introduction

This article is part of our State of the Web format we publish on a regular basis. Check out our other articles about the newest tech related stuff on the web.

Among a huge number of small improvements Node.js 10 offers better performance and new language features through an updated version of V8, the underlying JavaScript engine, a promisified file system API (goodbye callback hell) and a stable version of the N-API, the native interface to develop native C++ add-ons. Additionally, the new NPM version 6 with automatic dependency security audits and other handy features is already included.

Improved Performance and Features in Node.js 10

By upgrading V8 to version 6.6, Node.js 10 unlocks some performance improvements which are especially beneficial for code which uses promises and async generators. The chart below illustrates an interesting point: While code using async generators compiled down to backwards compatible language constructs using babel (promises and while loops) yielded comparable performance in the past, raw async generators are now more than three times faster than the compiled counterpart.

This highlights one important advice when dealing with modern JavaScript runtimes: Don’t compile features away unless you absolutely have to. Not only are they much more readable and concise; they also help the engine to better understand and optimize your code.

Async Generator Performance Improvements
Source: https://v8project.blogspot.com/2018/03/v8-release-66.html

Two notable new language features are the now optional exception parameter in catch-clauses

and the addition of String.prototype.trimStart() and String.prototype.trimEnd() for strings in addition to the already existing String.prototype.trim() function. There are many different helper libraries which provide such functions. My advice: Always use functions provided by the platform if possible as they are probably faster than anything possible in userland; on my machine String.prototype.trimEnd() is almost twice as fast as the corresponding lodash function.

New Experimental File System APIs

Seasoned JavaScript developers really know the pain of nested callback functions (also known as callback hell or pyramid of doom):

While there are various libraries to mitigate the problem and fortunately new APIs mostly use Promises or other modern language features, the very core APIs like fs are still stuck in dark API ages. Node.js 10 changes that by introducing the experimental fs/promises API set which is basically fs but all callbacks are swapped out with promises. The above example becomes:

—Shorter and much easier to read. While this might not look like a big deal, I think it’s great to see the Node.js team working on steadily cleaning up and modernizing the platform.

N-API

The N-API isn’t something the regular Node.js developer has to touch very often. But it is a crucially important piece of Node.js for some of the most widely used NPM packages. It comes into play every time native code is accessed from Node.js—which for instance is the case for talking to a MongoDB database or compiling Sass files.

In its essence it is a C/C++ API which allows existing C/C++ programs to provide a Node.js interface which makes the functionality accessible from ordinary Node.js JavaScript code. Prior to N-API such native add-ons had to talk directly to the underlying V8 engine—which meant each V8 update required all native addons to recompile to be able to interface with the V8-ABI.

N-API decouples addons from V8 and provides a stable API and ABI to develop and compile against which makes the life of addon developers much easier—plus you don’t have to re-install your local node_modules after a Node.js version update.

Deprecations and Cost Of Upgrading

There is a short list of breaking changes—features which were permanently removed in Node.js 10 while already being deprecated in documentation in previous releases.

The real hard breaking changes are:

  • Previously deprecated NODE_REPL_HISTORY_FILE environment variable has reached end-of-life and has been removed
  • Previously deprecated internal getters/setters on net.Server have reached end-of-life and have been removed
  • Previously deprecated legacy async_hooks APIs have reached end-of-life and have been removed

The full list of deprecated changes which shouldn’t be relied on anymore can be found on the Node.js website.

Most applications shouldn’t have any troubles meeting these minimal requirements to benefit from the advantages of Node.js 10. However, dependencies and transitive dependencies could theoretically pose a problem. A quick scan I did shows there are 8.465 out of 113.683 packages which specify compatible node versions that don’t support version 10. Yet only one of them is in the top 1.000 of packages most depended on. This means it is quite unlikely your app would break by upgrading.

Conclusion

Node.js 10 should be an easy upgrade with few breaking changes which in turn provides various improvements in different areas and opens the door for receiving new features in the future without any hassle. The platform continues to introduce well thought out features without failing to stabilize and clean up the existing system. This together with rising popularity and the omni-presence of JavaScript paints a rosy future for the ecosystem as a whole. I’m looking forward to Node.js 11!

One thought on “Node.js 10: Should you Upgrade? [State of the Web]

Hat dir der Beitrag gefallen?

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert