How is dart2js code faster than javascript?
Asked Answered
U

4

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.

Ureide answered 5/1, 2014 at 3:42 Comment(1)
Because it can use optimizations that you don't know about or are too lazy for.Kass
A
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.

Agency answered 5/1, 2014 at 3:48 Comment(4)
Does that mean they are somewhat misleading in their claim, then? Really it's "optimized JS" vs. "sloppy JS"?Rutabaga
@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.Alsatia
@ChrisHayes that makes sense. I suppose to be accurate they should have stated "dart2js optimized compiled JavaScript" vs. "JavaScript"Rutabaga
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.Photograph
P
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.

Pothouse answered 5/1, 2014 at 8:54 Comment(3)
Modern runtimes and JITs will gladly do this.Cleromancy
@BenjaminGruenbaum True, but wouldn't that fact that it's done ahead of time save the JIT/runtime a job?Seedy
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.Waly
R
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.

Radiocommunication answered 6/1, 2014 at 14:5 Comment(1)
This video is about Dart VM and V8, not Dart2js. It's an interesting video, but not very relevant to the question.Zygapophysis
W
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.

Wotan answered 5/1, 2014 at 18:37 Comment(2)
Relevant part of the video starts at about 7:30.Zygapophysis
There is an extended version of the Dart2js part of that presentation here: youtu.be/GwBb_nqQLucZygapophysis

© 2022 - 2024 — McMap. All rights reserved.