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! 🚀


State of Software Develoopment Report

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+)!

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.

Help us spread the love on social media!

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.


State of Software Develoopment Report

Update

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

This post has been featured on Awesome NodeJS.