r/javascript • u/magenta_placenta • Apr 26 '16
Node.js v6.0 released - supports 93% of ES6 features
https://nodejs.org/en/blog/announcements/v6-release/18
u/boiling_tunic Apr 27 '16 edited Apr 27 '16
Mmm delicious destructuring. An overview for those who are unfamiliar:
Old:
var result = getSeveralValuesInAnArray();
var a = result[0];
var b = result[1];New:
var [a, b] = getSeveralValuesInAnArray();For example:
Setup:
function min_max (arr) {
let min = Infinity; // Let keyword. ES6 feature (not new in Node 6.0.0)
let max = -Infinity;
for (let x of arr) { // For...of loop. ES6 feature (not new in Node 6.0.0)
if (x < min) { min = x; }
if (x > max) { max = x; }
}
return [min, max];
}Old style usage:
var results = min_max([1,2,3,4,5]);
var min = results[0];
var max = results[1];
console.log('Min: ' + min + '\nMax: ' + max);New style usage:
let [min, max] = min_max([1,2,3,4,5]); // Array destructuring. New feature!
console.log(`Min: ${min} \nMax: ${max}`); // String templating. ES6 feature (not new in Node 6.0.0)Aside: You can't run the new style code example alongside the old style code example as let will complain about redeclaring min and max
Edit: If anyone has any questions/feedback about any of the code in my post please let me know!
2
u/greim Apr 27 '16
Great example! A couple others I like:
var [ name, value ] = 'foo=bar'.split('='); var [ protocol ] = 'http://example.com/foo.html'.match(/^.*?:/);1
u/FancyCamel Apr 27 '16 edited Apr 27 '16
As someone that hasn't tried ES6 at all(but is curious), for your line:
var [ name, value ] = 'foo=bar'.split('=');What would happen if the string being split was 'foo=bar=something'?
My understanding is that, currently, it would assign variables so that name=foo and value=bar, correct? So what would happen in the situation above with 'something' ? Is this only a method that you can use when you know for sure that the number of values returned will match the number of variables you're creating?
2
u/boiling_tunic Apr 27 '16 edited Apr 27 '16
Any remaining parts are discarded
Edit: Installed Node 6.0.0 to make sure I wasn't lying to you!
1
u/greim Apr 27 '16
Yup, it's a contrived bit of example code that can definitely break on corner cases. It's nothing to do with ES6 actually, but rather the particular use of split().
38
u/hankDraperCo Apr 26 '16
Is anyone else pumped about destructuring? And default parameters?
9
u/cowjenga Apr 26 '16
Yes! Those two features alone (especially when combined) will make the first few lines of functions so much simpler/cleaner.
1
u/shanet $(this) Apr 27 '16
Far more than I should be. It's going to eliminate a lot of boilerplate. I'm curious what practices will emerge around parameter destructuring, I haven't seen much beyond simple examples so far.
1
13
u/Capaj Apr 26 '16
For me the single biggest feature are Proxies. I have even went ahead and implemented a small in memory DB experiment on top of them already. Very powerful stuff.
4
u/jcready __proto__ Apr 26 '16
That and the Reflect API are going to make for some awesome abstractions.
1
u/alessioalex Apr 27 '16
Be wary of performance though. Since it's just been newly released, it most like will be slow for a while until they optimize it.
Edit: I'm excited about Proxies too though.
2
u/ihsw Apr 27 '16
That's the most significant complaint about ES6 code -- it is currently much slower than ES5.
0
Apr 27 '16
[deleted]
1
u/Capaj Apr 27 '16
Yeah, it was. But I tried to reimplement a chunk of mobxdb with ES6 proxies and I liked how it worked out and decided to ditch mobx in favour of https://github.com/capaj/proxevable
Hence the rename.
8
Apr 26 '16
Performance improvements are key in this latest release with one of the most significant improvements coming from module loading, which is currently four times faster than Node.js version 4 (Node.js v4). This will help developers dramatically decrease the startup time of large applications for the best productivity in development cycles and more seamless experience with end users. In addition, Node.js v6 comes equipped with v8 JavaScript engine 5.0, which has improved ECMAScript 2015 (ES6) support. Ninety-three percent of ES6 features are also now supported in the Node.js v6 release, up from 56 percent for Node.js v5 and 50 percent for Node.js v4. Key features from ES6 include: default and rest parameters, destructuring, class and super keywords.
What does that mean when they say modules? Does that just mean the require() function or NPM or something else?
Edit
Nevermind: https://nodejs.org/dist/latest-v5.x/docs/api/modules.html
8
u/PitaJ Apr 27 '16
Some things left to implement:
Symbol.speciesIterator closing
Lots of prototype methods and prototype behaviors
ES6 Modules
2
u/dmtipson Apr 27 '16
Tail Call Optimization? Or is that in there already and I missed it?
3
u/TheNiXXeD Apr 27 '16
1
u/saadq_ Apr 27 '16
I don't believe it did, according to this at least.
4
u/TheNiXXeD Apr 27 '16
From my link:
Tail calling can be enabled by --harmony-tailcalls flag.
So it's not enabled by default.
3
2
u/Kratisto78 Apr 26 '16
Correct me if I'm wrong, but Koa required a flag until this was released. I've been planning to start my first node.js project, and I've been bouncing between Koa and Hapi. Does this push Koa a little ahead?
7
u/hankDraperCo Apr 26 '16
Koa said they aren't going stable with v2 until async/await wasn't behind a flag: https://github.com/koajs/koa/issues/533
async/await is still not a standard so that will still be awhile
1
u/Kratisto78 Apr 26 '16
Ahh that makes more sense. I figured I was wrong. Starting a new project with people that are all new to Node.js. Might just try Hapi to play it safe then. Thank you.
4
u/saadq_ Apr 26 '16
I believe Koa required a
--harmonyflag until Node v4.0.0. I think Koa is really going to gain in popularity once Node supportsasync/awaitwhich is what Koa v2 uses instead of generators.2
u/Kratisto78 Apr 26 '16
Makes sense. Hopefully, I can transition to that when it finally is supported.
2
0
Apr 27 '16
What are the breaking changes? I cannot find them on the changelog. Or are they ditching semver to celebrate?
2
u/tofagerl Apr 27 '16
Major releases CAN have breaking changes. They don't have to. That would be ridiculous.
-29
u/bart2019 Apr 26 '16
The version numbering scheme and especially the high speed by which the version number increases, is getting increasingly ridiculous.
What's this, a new major version for a few bug fixes, a speedup and some experimental features?
22
u/TheIncredibleWalrus Apr 26 '16
Node.js's versioning follows
semversemantics.Google it and study it (should take you about 3 to 5 minutes). You will stop being confused and you will also learn how 90% of all npm projects are being versioned.
6
u/sorahn on the cutting edge of cocking about Apr 26 '16
Considering its 4 months into the year AFTER the standard was finalized, I wouldn't call them "experimental".
2
u/ewan93 Apr 26 '16
I presume you haven't been burned by a package introducing breaking changes on a minor or patch version rather than major? Once you have, you appreciate semver more
1
u/tswaters Apr 27 '16
In a system such as this there may be many breaking changes -- if they released them immediately and followed semver, we'd be at version 1000 by now.
They batch them up with the tag 'semver-major' and every 6 months a new major version is released.
Each even numbered major version is LTS - you can realistically stay on LTS versions for a few years. Take a look at the LTS schedule for more information.
0
68
u/Cuel Apr 26 '16
I want es6 module support :(