Dieser Blogartikel ist älter als 5 Jahre – die genannten Inhalte sind eventuell überholt.
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.
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.
Two notable new language features are the now optional exception parameter in catch-clauses
and the addition of
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
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.
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.
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.