Features of Node 12

Node.js continues through its yearly release cycle with Version 12, which is code-named as Erbium. Since the version is even-numbered, it will enter as Long-term Support (LTS). It begins in October 2019 and spans till April 2022.
Node 12 comes with notable features and runtime upgrades. Node uses the V8 engine from Google, found in Chrome, and will incorporate the updates from that too.
Let us explore the features in Node 12:
V 8 Engine
A new version for V8 JavaScript Engine. It not only brings performance tweaks but has ongoing improvements for Node.js in areas of runtime and language. Some highlights will be:
- Async Stack Traces
- Faster Parsing of JavaScript
- Faster Calls with Arguments Mismatch
- Faster Await
Node 12 will initially run with V8 7.4 and subsequently upgrade to 7.6. The V8 team has agreed upon API (Application Binary Interface) stability for the above range. Some of the noteworthy improvements with V8 7.4 are:
- Better Memory Management
- Performance updates to ensure faster JavaScript execution.
- Broader ECMAScript Syntax Support
JavaScript Language Features
ASync Stack Traces
Till recently, the developers faced difficulty in truncating the stack trace till the recent await. The latest update enables Node.js to track the asynchronous call frames in error. stack property.
Here is a code snippet to show the above feature:
async function wait_pizzaready(x) { await wait_makesauce(x); } async function wait_makesauce(x) { await wait_makedough(x);} async function wait_makedough(x) { await x;}throw new error (“Oops”)}wait_1(1).catch(e => console.log(e.stack));
Private Class Fields
Node 12 comes with a private Class field, accessed from within the class itself, and not exposed externally. It is declared by prepending the variable with a # symbol.
Here is a code snippet to show the above feature:
lass foo { # b = 20;get( ) { return this.#b;}increment { ++this.#b;}}
The private class field throws a syntax error if access #name outside the class
const foo = new foo()foo.#name = 'NewName';// -> SyntaxErrorconsole.log(foo.#name)// -> SyntaxError
Public Class Fields
The new public class field simplifies the class definition. The listing of default values of a variable in the constructor, it defines them in the class level.
class IncreasingCounter { _count = 0; get value() { console.log('Getting the current value!'); return this._count; } increment() { this._count++; }}
The -count variable gets defined on top of the class. There is a need for a constructor to define fields. Here the -count field acted as property and prevented from accessing the property directly.
TLS and Security
TLS (Transport Layer Security) 1.3 is supported by Node 12 for reduced latency and increased security. It actively integrates across the web. The Node apps will have increased end-user privacy by implementing TLS 1.3. It also helps to improve the performance of requests, thereby reducing the time for HTTPS handshake. In addition to this, the crypto library has removed deprecated functions and TLS 1.0 and 1.1 disabled.
Supporting Import/Export Statement (No Bundler Required)
There is a path to the stability of modules inside Node 12 with Phase 3 of ECMAScript Modules. The import/export syntax is the preference of the JavaScript developers, since its standardization in ES6.
The experimental support on this feature started from Node 8.0 Phase 0 and has taken a step forward in Node 12. ECMAScript modules get support from all the major browsers, through the <script type=”module”>
Three types of imports are possible from the ES module files, working with all built-in Node packages by Phase 3 of the ECMAScript Modules.
You can import using the below export syntax while importing from the CommonJS package. The import syntax returns a promise and works with ESModules and CommonJS libraries. Files can load at runtime with dynamic import expressions.
import module from ‘cjs-library’.
Heap Size Improvements and Heap Dump functionality
In the previous releases for Node, unless configured, V8 had a default for maximum heap size to 700 MB or 1400 MB on a 32 or 64-bit platform, respectively. The update configures the JavaScript heap size on available memory instead of defaults set by V8, utilizing for the browsers.
While configuring the heap size based on memory, it gets ensured that Node.js does not try to use memory, more than that available. It quits with the memory exhaustion. The update becomes useful for the processing of large data sets.
Moreover, Node 12 brings the integrated heap dump capability – out of the box. It makes it easier to investigate memory issues.
Improvements in Native Modules and N-API
N-API offer native Node modules, for a native Node module system with stability. It offers ABI-stable abstraction over the native JavaScript APIs, by preventing breaking down of libraries in every release. N-API offers improvement in support to N-API, combining with worker threads.
Improvement in Startup Performance
In the previous version, Node.js 11 offers built-in code case support in workers. If any compilation of the library, in the main thread, then there is no need for a compilation of the worker thread, instead reuse the V8 Code cache, produced by the main thread, and speed up compilation. Similarly, you may reuse the cache generated by the workers. It provides a 60 % speedup for the startup of workers.
Now Node 12, allows building the code cache for built-in libraries in advance, at build time, and embeds in the binary. It helps in the final release. The main thread uses the code cache to start the initial load for any built-in JavaScript library. It provides a faster startup time for the main thread, by 30 %.
Switch Default HTTP Parser to llhttp
In this version, the default parser switches to llhttp. It allows comparison of llhttp-based implementation and testing easier.
Diagnostic Reports
There is a utility tool node-report brought into the node.js core. The report helps in memory leaks, high CPU usage, unexpected errors, and detection of abnormal terminations.
In the present versions of Node, many diagnostic utilities help in detection of errors and any bottlenecks, hard to pinpoint.
You get a JSON summary on heap statistics, resource usage, native stack traces by running:
node — experimental-report — report-on-fatalerror index.js
If the developer wants to get runtime statistics on heap usage, then call the following – added in v11.13.0.
v8.getHeapSnapshot()
Conclusion
2019 is a remarkable year for Node, with Node.js Foundation merged with JS Foundation to create the OpenJS Foundation.
The yearly updates in Node incorporate improvements to Node and JS Ecosystem. The hardworking team at Node surely provides no disappointment.
You may get a complete list of changes in Node 12 here.