13

I'm trying to better understand dart's effect on performance. On the dart website, their benchmarks show that Dart code compiled to Javascript is faster than just Javascript. How is this possible?

Tracer Benchmarks

I understand how the Dart VM is faster than v8, but what I don't get is how dart2js-generated javascript is faster than plain old javascript, when both are running in the same environment, v8.

hkk
  • 2,069
  • 1
  • 23
  • 48

4 Answers4

19

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.

Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
  • Does that mean they are somewhat misleading in their claim, then? Really it's "optimized JS" vs. "sloppy JS"? – DA. Jan 05 '14 at 06:45
  • 7
    @DA I think "sloppy" is an unfair mischaracterization. Even the best written C code is going to miss some optimizations that are obvious to a compiler, and anyone working on an ordinary project optimizes for maintainability. Their claim might be *slightly* misleading but on the whole I'd say it's accurate. – Chris Hayes Jan 05 '14 at 08:59
  • @ChrisHayes that makes sense. I suppose to be accurate they should have stated "dart2js optimized compiled JavaScript" vs. "JavaScript" – DA. Jan 05 '14 at 17:04
  • If I may be slightly pedantic - and perhaps proud - "Even the best written C code is going to miss some optimizations that are obvious to a compiler" isn't true as the starting point for me is usually compiled code with a variety of optimisations turned on, so if the compiler knows about it, I know about it too. Hand tuned code can beat good, clean compiled C by quite a wide margin, depending on the task in hand and how much time you are willing to pour into it. The best I have got is 50x over code that was already fast. Comp/JIT doesn't compete, but it is there for you when you lack time. – Max Murphy Oct 16 '15 at 09:02
6

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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 3
    Modern runtimes and JITs will gladly do this. – Benjamin Gruenbaum Jan 06 '14 at 14:28
  • @BenjaminGruenbaum True, but wouldn't that fact that it's done ahead of time save the JIT/runtime a job? – Hector May 10 '16 at 11:11
  • It's just a simple example. I'm not into compiler optimizations but some members of the Dart team are. All involved parties are constantly working on optimizations which will repeatedly lead into situations where optimizations dart2js made become obsolete because the JS VM does it already. The more people working on pushing the limits the better technology we all will get. – Günter Zöchbauer May 10 '16 at 11:27
4

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.

chameleon95
  • 234
  • 2
  • 8
  • 1
    This video is about Dart VM and V8, not Dart2js. It's an interesting video, but not very relevant to the question. – ntc2 Apr 17 '14 at 02:38
3

There is an interesting video by Seth Ladd and Kasper Lund. Kasper is involved in creating the Dart2js compiler and gives some code examples on how the compiler optimizes the Javascript code.

Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75