📜 ⬆️ ⬇️

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.


The new version has a lot of goodies and improvements runtime. In addition, given that the 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.


import/export syntax has become preferable when working with modules from js developers since standardization in ES6, and the team behind Node has been working diligently 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 already support this feature with <script type="module"> .


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


 //    import module from 'module' //   import { namedExport } from 'module' //     import * as module from 'module' 

Vanilla modules can only be exported 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 => console.log(error.stack)); 

On older versions you will 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 does not mention testAsyncStacktrace . 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 the case of an 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 arguments object, 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 while they are being loaded. V8 7.4 fixed a problem with the decoding performance of UTF-8, which resulted in an 8% acceleration.



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


Improved await


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}`); } } 

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


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

Fast 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 flow, 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 has greatly changed the protocol and is actively integrating 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 handshake in HTTPS. In addition, TLS 1.0 and 1.1 are disabled by default, and deprecated methods have been removed from crypto .


Dynamic hip 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 dumping 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 feature for the reports.


Improvements when working with native modules


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


Conclusion


Node 12 has a lot of improvements. Full CHANGELOG can be viewed on Giethab and on the site itself.


')

Source: https://habr.com/ru/post/450292/


All Articles