BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Dart2js Outperforms Hand-Written JavaScript in DeltaBlue Benchmark

Dart2js Outperforms Hand-Written JavaScript in DeltaBlue Benchmark

Leia em Português

This item in japanese

Google released a new version of the Dart2js compiler, whose generated JavaScript code now outperforms hand-written JavaScript in the DeltaBlue benchmark, a commonly used benchmark for benchmarking object-oriented languages. While outperforming hand-written JavaScript was never a goal of the Dart2js project, the optimizations applied, such as method inlining and pre-computation, result in some Dart programs running faster than equivalent idiomatic JavaScript code. While the numbers are slowly converging for the Richards benchmark, the other benchmark that the Dart site publishes, the Dart-generated JavaScript performance for the Richards benchmark is still 26% slower than hand-written JavaScript.

The following graph shows how performance of Dart on the DeltaBlue benchmark has progressed over time:

The purple line in this graph represents JavaScript generated by Dart2js running on Google's v8 JavaScript engine. The yellow line represents equivalent idiomatic JavaScript code running on v8. The blue line at the top represents the Dart code running natively on the Dart VM. The higher the number, the better the performance.

Dart, Google's new language for developing large web applications, can be run in various contexts:

  1. In a browser-embedded Dart VM. Since the Dart project has not yet released a 1.0 version, there is no browser available today that embeds the Dart VM other than the "Dartium" build of Chromium that comes with the Dart SDK.
  2. On the server, running in the Dart VM. The dart:io library, only available for server-side applications, offers APIs for accessing the file system, process management and building servers (e.g. HTTP servers or websocket servers). This allows Dart to be used for very similar use cases as Node.js and enables front-to-back application development in Dart.
  3. Embedded in applications. The Dart VM can be embedded in arbitrary (C/C++) applications, to support scripting the application with Dart.
  4. Compiled to JavaScript in any modern browser. No production browser supports Dart today, nor is it clear whether browsers other than Chrome will embed the Dart VM. To still be able to run Dart code in these browsers, the Dart2js compiler compiles Dart programs to JavaScript. Therefore, the performance of this generated code is vital to Dart's chances of success.

The new Dart2js compiler, itself implemented in Dart, uses a technique called global type inferencing to gather more information about the types that variables and arguments are going to have. As a result it can generate more compact and faster JavaScript code. Interestingly, Dart2js does not use the optional type annotations that Dart supports. The reason for this is that these types are not enforced at run-time, unless Dart is run in checked mode, which does report errors when the type constraints are invalidated. Therefore, it is perfectly valid, though confusing, to write a statement like String name = 10;. To ensure correctness of the generated code, the compiler ignores type annotations completely.

Many of the new optimizations are possible because Dart is a less liberal language than JavaScript. For instance, in JavaScript it is possible to dynamically add methods to objects, to replace methods, to dynamically download code, and to use eval and the with statement, greatly reducing the amount of optimization a JavaScript VM like v8 can perform. Dart does not support many of these capabilities. Therefore, when the Dart2js compiler runs, it will know exactly what code will be running. It can eliminate code that is not used from the output, a process called dead-code elimination or tree shaking. It can also inline methods in some cases, because unlike JavaScript, Dart does not support monkey patching objects.

While any benchmark should be taken with a grain of salt, it is interesting to see how the Dart teams keeps improving their performance numbers. While Dart is still under heavy development, it starts to see some early adoption. The performance and size of Dart2js generated code is important to everybody interested in deploying Dart code on the web today.

Rate this Article

Adoption
Style

BT