The original address: weekly. Shanyue. Tech/release/nod…

Node16 was released on April 20, and Yamatsuki downloaded Node16 for the first time to experience the following new features. The even-numbered version will be the LTS version, which will be the next long-term support for Node14. Node14 will be Active LTS as of 2020-10-27.

The official Node 16 Release documentation lists all of the new features and provides a very detailed commit history. Let’s take a look at some of the new features.

Timers Promise API

The Timers Promise API has actually been around since Node15, as an experimental feature, and is an exciting feature that has now reached a stable stage. So what exactly is it for?

Before we do that, let’s look at a question: How to implement a sleep/delay function?

The answer is as simple as encapsulating a setTimeout with a Promise

const sleep = (t = 0) = > new Promise(resolve= > setTimeout(resolve, t));
Copy the code

What about with Timers Promise? SetTimeout can directly replace the sleep function, which is not a surprise.

import { setTimeout } from 'timers/promises'

await setTimeout(100)
Copy the code

The second argument to setTimeout accepts a value that will be returned if the Promise succeeds

const r = await setTimeout(100.'hello, world) //=> hello, world console.log(r)Copy the code

When setInterval also becomes a Promise, it becomes more readable for timed tasks that perform actions every minute

import { setInterval } from 'timers/promises'

for await (const startTime of setInterval(100.Date.now())) {
  const now = Date.now()
  if ((now - startTime) > 1000)
    break
}
Copy the code

Low-level dependency escalation

As we know, Node is based on v8, libuv, LLHTTP, etc., and this time it has updated many of these dependencies. Just as our business projects rely on a number of packages, each dependent upgrade can result in a significant increase in performance

  • V8, upgraded to 9.0, mainly ECMAScript RegExp Match Indices
  • LLHTTP, upgraded to 6.0.0, used to parse HTTP packets
  • Icu, upgrade to 69.1
  • NPM, upgrade to 7.10.0

Use process.versions to see the version number of the dependency

> process.versions
{
  node: '16.0.0'.v8: '10' 9.0.257.17 - node..uv: '1.41.0'.zlib: '1.2.11'.brotli: '1.0.9'.ares: '1.17.1'.modules: '93'.nghttp2: '1.42.0'.napi: '8'.llhttp: '6.0.0'.openssl: '1.1.1 k + quic.cldr: '39.0'.icu: '69.1'.tz: '2021a'.unicode: '13.0'.ngtcp2: '0.1.0 from DEV -'.nghttp3: '0.1.0 from DEV -'
}
Copy the code

Btoa and atob

For Base64 conversion, Node previously used buffer. from, but now supports BTOA/ATOB in line with the browser environment.

For an SSR project, the distinction between the execution environment will not matter, and bTOA/ATOB will be used uniformly

const base64 = {
  encode (v: string) {
    return isBrowser ? btoa(v) : Buffer.from(v).toString('base64')
  },
  decode (v: string) {
    return isBrowser ? atob(v) : Buffer.from(v, 'base64').toString()
  }
}
Copy the code

More and more

For details on the Node16 upgrade, see the release release for more information, and we’ll see you in the next update