βœ•
_ Blog / Technology

Node 8: Five New Features You Need to Know

by Ákos Szokodi
/ May 29, 2017
#Technology
Node 8

Node 8 is out on May 30th (a bit later than expected) and packed with new ES features along with performance improvements.

The reason of the delay is actually a performance concern: because of the staggered release, the Node team is able to ship version 8 with an updated V8 engine featuring a more modern compiler + jit pipeline.

Version 8 is important as later this year (in October) Node 8 will be LTS, so it's worth checking out its new features compared to the previous LTS (v6) and the previous stable (v7) release.

Let’s get started! πŸš€

FIVE NODE 8 FEATURES:

1. STRING PADDING

  • v6: no β›”

  • v7: harmony πŸ’›

  • v8: yes πŸ’š

PadStart and PadEnd functions are basically string utility functions which are already used by the majority of JavaScript developers today, provided by various libraries such as lodash.

In fact, one time padStart (or as they called it left-pad) was unpublished from NPM and lots of packages broke that day that depended on it (like React or Babel).

// String.prototype.padStart(targetLength [, padString])
'hello'.padStart(10); // '     hello'
'hello'.padStart(10, '0'); // '00000hello'
'hello'.padStart(); // 'hello'
'hello'.padStart(6, '123'); // '1hello'
'hello'.padStart(3); // 'hello'
'hello'.padStart(3, '123'); // 'hello';

// String.prototype.padEnd(targetLength [, padString])
'hello'.padEnd(10); // 'hello     '
'hello'.padEnd(10, '0'); // 'hello00000'
'hello'.padEnd(); // 'hello'
'hello'.padEnd(6, '123'); // 'hello1'
'hello'.padEnd(3); // 'hello'
'hello'.padEnd(3, '123'); // 'hello';

2. TRAILING COMMA IN PARAMETER + ARGUMENTS

  • v6: no β›”

  • v7: no β›”

  • v8: yes πŸ’š

In early Node versions (0.X) the support for object and array trailing commas were already implemented, making multiline object and array definitions much more friendly towards extension and version control.

In Node 8 this support arrived for function parameters and function arguments too. From now on, the definition and invocation of multiline parametered functions will now also be extension and version control friendly.

function foo(
  a,
  b,
  c, // this threw a Parse Error before Node 8
) { … }

foo(
  ‘a’,
  ‘b’,
  ‘c’, // this threw a Parse Error before Node 8
);

3. ASYNC FUNCTIONS

  • v6: no β›”

  • v7.5: harmony πŸ’›

  • v7.10: yes πŸ’š

  • v8: yes πŸ’š

Back in 2015, when Node 4 was introduced, generators were implemented from the ES2015 standard. With a package called co we could utilize generators to write better asynchronous code, by ‘yielding’ promises instead of chaining them with ‘then’, creating a more readable control flow.

const co = require('co');
const fetch = require('node-fetch');

function * getUser(username) {
  const res = yield fetch(`https://api.github.com/users/${username}`);
  const json = yield res.json();
  return json;
}

function * start() {
  const user = yield getUser('github');
  console.log(user); // prints out info about ‘github’ user on GitHub
}

co(start);

The async/await feature brings this approach to a new level. This control flow can be implemented without the need of a third party library. Instead of generator functions we use async functions, and instead of the yield keyword we use the await keyword.

Brain Meme
const fetch = require('node-fetch');

async function getUser(username) {
  const res = await fetch(`https://api.github.com/users/${username}`);
  const json = await res.json();
  return json;
}

async function start() {
  const user = await getUser('github');
  console.log(user); // prints out info about ‘github’ user on GitHub
}

start();

If you are a Koa fan and coming from Node 6 where async/await was not available, good news: Koa 2 can be used with Node 8 (as it requires at least version 7.6+)!

πŸ‘‰Reading Suggestion: How to Easily Set-up Node Config Following These Best Practices πŸ“—πŸ“˜πŸ“™

4. TEMPLATE LITERAL REVISION (ES2018)

  • v6: no β›”

  • v7: no β›”

  • v8: harmony πŸ’›

Template literals were introduced in the ES2015 standard. It is really useful when it comes to string interpolation, however there are some special unicode characters, such as \u or \x that cannot be used in template literals.

The template literal revision aids this problem by allowing these special characters. A good use case can be found in the proposal document: the embedding of DSL languages (like LaTeX where \u or \x characters are common) can greatly benefit from this feature.

function latex(strings) {
  // ...
}

latex`
\document{article}
\usepackage{somepackage}
`
// \document{article} works on previous Node versions
// \usepackage{somepackage} will only work on Node 8 due to ‘\u’ special character

5. OBJECT REST AND SPREAD (ESNEXT)

  • v6: no β›”

  • v7: no β›”

  • v8: harmony πŸ’›

Array rest and spread were already available in previous Node versions. Now in Node 8 the support for object rest and spread has landed.

Object rest gathers the non-destructured keys remaining in the object:

const foo = { a: 1, b: 2, c: 3 };
const { a, ...rest } = foo;
console.log(rest); // { b: 2, c: 3 }

Object spread inserts the respective object’s key-value pairs into the target object:

const foo = { a: 1, b: 2, c: 3 };
const bar = { d: 4, e: 5 };
const baz = {};

const qux = { ...foo, ...bar, ...baz };
console.log(qux); // { a: 1, b: 2, c: 3, d: 4, e: 5 }

Spread can be used instead of Object.assign and/or lodash assign/extend. Both rest and spread helps to create a more readable codebase.

BONUS: NPM 5

Although not strongly coupled with Node, it's worth noting that NPM version 5 is out. Some cool features like automatic --save flag is applied upon package install, or the debut of package-lock.json lockfile. For the full feature list check out the NPM blog.

CONCLUSION

That’s it! With this post, we wanted to give you a list of the new Node 8 features to help you quickly review the most important changes. If there is anything new to add feel free to let us know in the comments below.

FURTHER READING πŸ“—πŸ“˜πŸ“™:

πŸ‘‰ How to Easily Set-up Node Config Following These Best Practices

πŸ‘‰ Define Mongoose Models Using TypeScript Classes

πŸ‘‰ Comprehensive Guide to Code Quality: Best Practices and Tools

State of Serverless 2020

Update

Brain meme got "sorted out" :)
Object and array trailing comma fact fix.

This post has been featured on Awesome NodeJS.