Introducing Node.js 6.0

Free Course

Build Your First Node.js Website

Node is a powerful tool to get JavaScript on the server. Use Node to build a great website.

Wait, What? About six months ago, Node Foundation announced version 5, well now its time to upgrade again. This new release of node aims to improve performance, reliability, usability and security.

Who needs to Upgrade?

0.12, 0.14, 5.* (where represents any sub-version). If you are using any of these versions of node, you might want to consider upgrading your node install. Since 4.0 is LTS (long-term support), you don't have to upgrade yet as `4.` will have support until April of 2017.

Performance Upgrades/Improvements

The first thing you should know about any perfomance improvements is that, node now runs on the latest v8 engine.

Next is module loading speed. We have not run any tests, but node foundation has and claims that module loading is now four times faster.

According to node green, Node 6.0 supports 93% of ES6 (Ecmascript 6) features.

Notable Fixes

Buffer

  • There is a new Buffer API that reduce the risk of bugs and vulnerabilities leaking into applications.
  • Deprecation of the current Buffer and SlowBuffer APIs.
  • A new lastIndexOf method has been added to Buffer prototypal chain.

and many more.

New ES6 Features

With this new release, there are some features of es6 that we can use in Node. Speaking of improving usability, these features sure do go a long way to see to that.

Default Function Parameters

This has been a long-awaited feature in node.js. Before this release, developers would do this to make sure that an argument has a default value.

function functionWithDefaultValue(val) {
    val = val || {};
}

Now, we can just do this.

function functionWithDefaultValue(val = {}) {

}

Spread Operator

This new operator lets us extract each array item as function parameter. Before the spread operator, we would do something like this.

function myFunc(x, y, z) {

}

var args = [0, 1, 2];
myFunc.apply(null, args);

With the spread operator, we can now do something like this.

function myFunc(x, y, z) {

}

var args = [0, 1, 2];
myFunc(...args);

Rest Parameters

If we wanted to have variadic function in javascript, we use to make use of arguments, now we can use the spread operator to take in many parameters.

Before.

function variadic() {
    console.log(arguments);
}

variadic(1, 22, 12, 13);
variadic('adfsd', {}, [1, 12]);

Now

function variadic(...args) {
    console.log(args);
}

variadic(1, 22, 12, 13);
variadic('adfsd', {}, [1, 12]);

Well, what's the difference? arguments take all the parameters passed to the function. With this new operator, we can pass different variables before passing the spread operator.

function argumentsDemo() {
    console.log(arguments);
}

argumentsDemo(2, 2, 24, 2, 4, 4, 2); // logs 2, 2, 24, 2, 4, 4, 2

function spreadDemo(a, b, ...spread) {
    console.log(a, b);
    console.log(spread);
}

// after the first two variables, we can add as many as we want.
spreadDemo(1, 3, '24', 'lorem');
// logs
// 1 3
// 24, lorem

Destructuring

The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects into distinct variables.

var x = [1, 2, 3, 4, 5]  
var [y, z] = x  
console.log(y) // 1  
console.log(z) // 2  

It can also be used to swap variables

var a = 1;
var b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

Read more on destructuring.

Proxy

The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

In this simple example, the number 37 gets returned as the default value when the property name is not in the object. It is using the get handler.

var handler = {
    get: function(target, name){
        return name in target ? target[name] : 37;
    }
};

var p = new Proxy({}, handler);
p.a = 1;
p.b = undefined;

console.log(p.a, p.b); // 1, undefined
console.log('c' in p, p.c); // false, 37

More on proxies;

Reflect

For those of you who write code in Java, PHP, or something similar, you might have head or reflection. Relect is Javascripts / nodes reflection. According to MDN

Reflect is a built-in object that provides methods for interceptable JavaScript operations.

Symbols

Symbols are unique and immutable data type that may be used as an identifier for object properties. No two symbols can have the same value. For example.

Symbol('scotch') !== Symbol('scotch');

Read this article to learn more about es6 symbols.

When Should I Upgrade

At the moment, they are still trying to fix some kinks in the release — but by October of this year (2016), node 6.0 will be an LTS. Right now, if you want to play with the latest features, go right ahead. But that doesn't mean you can't start upgrading your codebase.

Samuel Oloruntoba

Self-proclaimed full-stack web developer and a quasi-academic. I work mostly on the backend (PHP and Node) with a recent enthusiasm for frontend development (React, SVG, HTML5 Canvas).