The biggest javascript news in recent days is the official release of KOA2. The latest version is v2.0.1, and the KOA2 team announced that “koA2 will not be released as long as NodeJS does not support Async.” With the arrival of Node V7.6.0, Koa2, which full stack engineers have been waiting for, is now available, with perfect support for async functions and elegant handling of asynchronous callbacks.

And that’s thanks to the V8 engine. The question about async on Zhihu appears again on timeline: What is the implementation principle of async/await proposal in ES Next? I saw many people reply that async is a promise, yield, or generator, so I joined in and answered. When I looked at it again in the evening, I suddenly felt a sense of time travel:

V8 has been getting a new TurboFan optimization engine for a long time now, and not just for try/catch/finally, for… Previously Crankshaft of etc. could not optimize the syntax and also supported more ES2015+ features.

TurboFan engine has not only added async support, but also async, generators optimization support. Here are the generated binaries and TurboFan executables:

Many of the new ES2015 features that require compiling with Babel are supported natively.

Asynchronous functions:

async function* readLines(path) {
  let file = await fileOpen(path);
  try {
    while(! file.EOF) {yield awaitfile.readLine(); }}finally {
    awaitfile.close(); }}Copy the code

Babel compiles the above 187 characters into 2,987 characters, increasing the code size by 650%. The file address: gist.github.com/justjavac/c…

Destructuring assignment of arrays and objects (Array destructuring)

function fn() {
  var [c] = data;
  return c;
}Copy the code

Bable compiles to:

"use strict";

var _slicedToArray = function () {... (omitted here630Char)function fn() {
  var _data = data,
      _data2 = _slicedToArray(_data, 1),
      c = _data2[0];
  return c;
}Copy the code

Looking at the full code you’ll find try/catch/finally that Crankshaft can’t optimize. In V8, however, destructor assignment is almost as fast as direct assignment to array elements:

function fn() {
  var c = data[0];
  return c;
}Copy the code

Other ES2015+ features have also been significantly improved:

Check out ES2015 and Beyond Performance Plan for V8 to keep up with the latest V8 developments.

Reference article:

  • High-performance ES2015 and beyond
  • Async Iterators for JavaScript
  • V8’s new interpreter and compiler pipeline
  • ES2015 and beyond performance plan

Finally, commercial time, welcome to subscribe to my official account:

justjavac V8