What's new in Node 12

What's new in Node 12


Recently Node 12 was released with the code name Erbium , whose long-term support (LTS) will last from October 2019 to April 2022.


In the new version of a lot of goodies and improvements runtime. In addition, given that V8 is under the hood, the node will also receive all the engine improvements.




import/export


support

The node enters phase 3 on the way to ECMAScript Modules . Initially, this feature was available only with the - experimental-modules flag. By the time of transition of Node to LTS it is planned to remove the need to use this flag.


The syntax using import/export has become preferable when working with modules from js developers since standardization in ES6, and the team behind Node has been diligently working on native support. Experimentally, this feature was available from Node 8.0 with phase 0. The current release is a big step in that direction. Most popular browsers support this feature with & lt; script type = "module" & gt; .


From Phase 3 there will be support for three import options from the ES model:


 //default export
 import module from 'module'
//named export
 import {namedExport} from 'module'
//export the entire namespace
 import * as module from 'module'  

Vanilla modules can be exported only by default:


 
 import module from 'cjs-library'
  

You can use import () to load in runtime. import () returns Promise and works with both ES models and CommonJS libraries.


V8


Node 12 will initially run on V8 7.4 and will eventually upgrade to 7.6. The V8 team agreed to provide ABI (Application Binary Interface). Notable improvements in V8 7.4 are performance improvements for faster JavaScript execution, better memory management, and enhanced ECMAScript syntax support.



Async stack traces


Let's look at this code:


 
 async function testAsyncStacktrace () {
  await killme ();
  return 42;
 }

 async function killme () {
  await Promise.resolve ();
  throw new Error ('# Feelsbadman');
 }

 testAsyncStacktrace (). catch (error = & gt; console.log (error.stack));
  

On older versions, you get something like this:


  Error: #Feelsbadman
  at killme (test.js: 8: 11)
  at process._tickCallback (internal/process/next_tick.js: 68: 7)
  at Function.Module.runMain (internal/modules/cjs/loader.js: 721: 11)
  at startup (internal/bootstrap/node.js: 228: 19)
  at bootstrapNodeJSCore (internal/bootstrap/node.js: 576: 3)  

As you can see, the message doesn’t mention testAsyncStacktrace anywhere.And now the - async-stack-traces flag is enabled by default, and the log will look like this:


  Error: #Feelsbadman
  at killme (test.js: 8: 11)
  at async testAsyncStacktrace (test.js: 2: 5)
  

Accelerated call if the number of arguments does not match


In JavaScript, it is perfectly acceptable to call functions with fewer/more arguments (i.e., pass less or more declared formal parameters). In the first case, this is under-application , and in the second, over-application . In case of insufficient number of arguments, the parameters will be undefined , whereas in the case of a large number of parameters, they are simply ignored.


However, JavaScript functions can still retrieve actual parameters using the object. arguments , rest parameters or even using non-standard Function.prototype.arguments in sloppy mode functions. As a result, JavaScript engines must provide a way to get the actual parameters. In V8, this is done using the arguments adaption technique. Unfortunately, similar methods affect performance.


In some scenarios, V8 completely skips the arguments adaption , reducing call charges by up to 60%.



Details can be found in the document .


Accelerated Parsing


In Chrome, quite large scripts are streamlined in workflows as they load. V8 7.4 fixed a problem with the decoding performance of UTF-8, which resulted in an 8% acceleration.



Each drop in the diagram is one of the performance improvements in the stream analyzer.


await


improved

Together with the - async-stack-traces flag, the - harmony-await-optimization flag is now enabled by default. Details here .


Private class fields


Available in V8 the ability to use private fields migrated to the node. Such fields are not available outside the class. To create these, you need to specify # in front of the variable.


 
 class HelloThere {
  #hidden = 'Hidden';

  get hidden () {
  return this. # hidden;
  }

  set hidden (txt) {
  this. # hidden = txt;
  }

  hi () {
  console.log (`Hello, $ {this. # hidden}`);
  }
 }
  

When you try to access #hidden from the outside, get a syntax error.


 
 const hello = new HelloThere ();
 hello. # hidden = 'Visible';//- & gt;  SyntaxError
 console.log (hello. # hidden);//- & gt;  SyntaxError
  

Quick Start


Node 12 will use the cache for the built-in libraries before building it and embed it as a binary. Due to the use of this cache by the main thread, the start time will be reduced by 30%.


TLS and security


The node now supports TLS 1.3, offering enhanced security and reducing latency. TLS 1.3 strongly changed the protocol and is actively integrated into the network. The introduction of TLS 1.3 will increase the confidentiality of user data, as well as accelerate the processing of requests by reducing the time for a handshake in HTTPS. In addition, TLS 1.0 and 1.1 are disabled by default, and deprecated methods have been removed from crypto .


Dynamic Heap Size


Previously, the default V8 heap size was used, which is 700MB (32-bit systems) or 1,400MB (64-bit systems). Now the node will determine the size of the heap based on the available memory in order to optimize the used resources of the machine.


Ability to dump hip


Node 12 provides the ability to dump heaps, making it easier to detect memory problems. Details can be found here and here .


Experimental Diagnostic Reports


The node offers more powerful tools for diagnosing problems (performance, CPU utilization, memory, crashes, etc.) right inside the applications, providing an experimental report feature.


Improvements when working with native modules


Node 12 continues the trend to simplify working with N-API . This version has improved support, particularly when working with worker threads.


Conclusion


Node 12 has a lot of improvements. You can view the full CHANGELOG at GitHub and on site .

Source text: What's new in Node 12