How is dart2js code faster than javascript? How is dart2js code faster than javascript? dart dart

How is dart2js code faster than javascript?


dart2js is able to perform optimizations that would not normally be manually added in JavaScript code.

There is nothing particularly special about Dart being the source language in this case: any automated tool that generates JavaScript should be able to do this, for instance the GWT compiler (Java to JavaScript) does this as well. Of course, you can run automated tools on JavaScript source to generate better JavaScript as well, this is what the Closure compiler does.

Technically, you can manually achieve the same speed with handwritten JavaScript if you know all the tricks.


One example is function inlining. If you need a code fragment called repeatedly you would refactor it out in a function/method. Dart2js often does the opposite. Method calls often get replaced with the code fragment contained by the called function/method which is called inlining. If you would do this manually this would lead to unmaintainable code.

I think many of the optimizations go in that direction.Writing the code that way would just be unreadable and thus unmaintainable.This doesn't mean it's sloppy.


There is a great presentation by Vyacheslav Egorov from the dart team where he explains in detail some of the optimizations including in lining..

http://www.infoq.com/presentations/dart-compiler

Summary Vyacheslav Egorov details how some of Dart's language features affected the design of a new JIT Dart compiler and how the V8 JavaScript engine influenced the overall design.