Friday, July 17, 2015

V8 4.5 release

Roughly every six weeks, we create a new branch of V8 as part of our release process. Each version is branched from V8’s git master immediately before Chrome branches for a Chrome Beta milestone. Today we’re pleased to announce our newest branch, V8 version 4.5, which will be in beta until it is released in coordination with Chrome 45 Stable. V8 4.5 is filled will all sorts of developer-facing goodies, so we’d like to give you a preview of some of the highlights in anticipation of the release in several weeks.

Improved ECMAScript 2015 (ES6) support

V8 4.5 adds support for several ECMAScript 2015 (ES6) features.

Arrow Functions

With the help of Arrow Functions it is possible to write more streamlined code.

1
2
3
4
5
6
let data = [0, 1, 3];
// Code without Arrow Functions
let convertedData = data.map(function(value) { return value*2;});
// Code with Arrow Functions
let convertedData = data.map(value => value*2);
console.log(convertedData);

The lexical binding of 'this' is another major benefit of arrow functions. As a result, using callbacks in methods gets much easier.

1
2
3
4
5
6
class MyClass {
  constructor() { this.a = "Hello, "; }
  hello() { setInterval(() => console.log(this.a + "World!"), 1000); }
}
let myInstance = new MyClass();
myInstance.hello();

Array/TypedArray functions

All of the new methods on Arrays and TypedArrays that are specified in ES2015 are now supported in 4.5. They make working with Arrays and TypedArrays more convenient. Among the methods added are Array.from and Array.of. Methods which mirror most Array methods on each kind of TypedArray were added as well.

Object.assign

Object.assign enables developers to quickly merge and clone objects.

1
2
3
4
5
let target = {a: "Hello, "};
let source = {b: "world!"};
// Objects are merged
Object.assign(target, source);
console.log(target.a + target.b);

This feature can also be used to mix in functionality.

More JavaScript language features are “optimizable”

For many years, V8’s traditional optimizing compiler, Crankshaft, has done a great job of optimizing many common JavaScript patterns. However, it never had the capability to support the entire JavaScript language, and using certain language features in a function—such as “try/catch” and “with”—would prevent it from being optimized. V8 would have to fall back to its slower, baseline compiler for that function.

One of the design goals of V8’s new optimizing compiler, TurboFan, is to be able to eventually optimize all of JavaScript, including ECMAScript 2015 features. In V8 4.5, we’ve started using TurboFan to optimize some of the language features that are not supported by Crankshaft: ‘for-of’, ‘class’, ‘with’ and computed property names.

Here is an example of code that uses 'for-of', which can now be compiled by TurboFan:

1
2
3
4
5
6
let sequence = ["First", "Second", "Third"];
for (let value of sequence) {
  // This scope is now optimizable
  let object = {a: "Hello, ", b: "world!", c: value};
  console.log(object.a + object.b + object.c);
}

Although initially functions that use these language features won't reach the same peak performance as other code compiled by Crankshaft, TurboFan can now speed them up well beyond our current baseline compiler. Even better, performance will continue to improve quickly as we develop more optimizations for TurboFan.

V8 API

Please check out our summary of API changes. This document gets regularly updated a few weeks after each major release.

Developers with an active V8 checkout can use 'git checkout -b 4.5 -t branch-heads/4.5' to experiment with the new features in V8 4.5. Alternatively you can subscribe to Chrome's Beta channel and try the new features out yourself soon.

Posted by the V8 team

3 comments:

  1. Why ever optimize for "with"? It is hazardous and people should not use it.

    ReplyDelete
    Replies
    1. Please be aware that 'with' is not optimized. The code in the 'with'-block now has the potential for optimization. See above 'for-of' example to see the slight difference.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete