
"Hello World" in Dart, compiled to JavaScript - brodd
https://gist.github.com/1277224
======
sjs
As me1000 (Randy Luecke) says Dart is not meant for hello world. If you run
this through closure-compiler it's reduced to 2000 lines which is nothing next
to Underscore, jQuery, Dojo, etc.

We all know the benefits of offloading code to a well known and well tested
library or framework, especially if you're writing an app that is thousands of
lines anyway. How is this contentious at the end of 2011 unless you're just
hating Dart for the sake of it?

I haven't looked into the language yet and unless you haven't either let's all
refrain from judgements based on this silly metric alone. Circle jerks and
knee-jerk reactions are for Reddit, not HN.

Don't get me started on the whining about math performance. Reminds me of
dinosaurs whining about how assembly is better than C is better than Java is
better than.... There is more to software development than math. Crazy, new
fangled, inefficient languages eat the lunch of older languages every single
time. You have to be insane to pull out that gem. If math is your bottleneck
then don't use Dart, if you know that math is your bottleneck then you
shouldn't have to be told this! Sorry for the rant but half of the complaints
I've read about Dart are utterly ridiculous, and speculative to boot.

~~~
chrisohara
..and here's the jsperf for the --optimize'd hello world vs. vanilla JS -
<http://jsperf.com/dart-hello-world-test-with-optimize/2>. 17k vs. 4M for me

~~~
sjs
Got any useful benchmarks? Nobody is wondering which language to write their
next "hello world" app in.

Just for the record I'm not saying Dart is amazing and everyone should use it,
all I'm saying is that we should evaluate it objectively. If raw speed is your
bottleneck then why are you wasting time evaluating Dart? Nobody is forcing
anyone to use it, you can ignore it and move on with your day.

~~~
chrisohara
Dart won't be supported ubiquitously any time soon (if ever) which is why
there'll always be a Dart->JS compiler. Why not just stick with vanilla JS or
Harmony/ES6? I'd benchmark some bigger apps but they don't exist yet

------
KirinDave
So the joke is that a prototype of a whole separate language has to include
its runtime in its cross-compiled output? Did people... think this wouldn't be
the case? Do they think that Javascript is not thousands of lines of C/C++
lurking in your browser?

Good joke guys, but I don't think you realize which way the punchline is
pointing...

It sort of amazes me that people are being so hard on Dart, given that they
should be grasping for _any chance_ to leave the warty and dated pile of
compromises that is Javascript behind. Even the javascript community seems to
tacitly admit that Javascript is a bit of diamond amongst a whole lot of
dross; hence CoffeeScript and books like "Javascript: The Good Parts".

~~~
devin
Amen. The "me too" culture that's going on w/r/t Dart is really disappointing.

Google: "Hey everyone, let's make the browser better by switching to something
that smells less like garbage than javascript."

Community: "You forgot a semi-colon!"

Shameful.

~~~
msy
Sorry but it's been more like

Google: Hey everyone, we have decided the future, here it is, get used to it.

Community: Maybe you should get behind Harmony instead of secretly building
replacements to the building blocks of the web and issuing them by imperial
decree.

~~~
devin
There was an internal memo. They're going to push it. Why do you have to "buck
the man" in order to feel like you're in control of your tools? Why does it
need to be repeated that everyone has a choice?

If Dart grows and solves some of the junk I have to deal with in the JS
ecosystem, I will use it. We'd all be fools not to.

~~~
msy
Wouldn't happen to be a Flash dev would you?

I do not want the core technologies on which the web is built owned or
controlled by any one company. No matter how you look at it Dart is not a
standard, it's not controlled or directed by any open body and its development
is not public.

It took a goddamn decade to break Microsoft's endless attempts to own core web
technology, if you're happy with another company doing it just because you
think they're a 'nice' company you're the fool.

There is a large, public and active effort to solve many of the issues JS has,
it's called Harmony. Maybe you should get involved with that instead of
jumping on a proprietary bandwagon.

~~~
georgemcbay
"Dart is not a standard, it's not controlled or directed by any open body"

You say that like it is a bad thing. I don't think it is.

Standardizing technologies is great once they are mature and are already
accepted as the obvious de-facto standard, but trying to push real change
through standards bodies results in exactly the sort of morass that JavaScript
has been stuck in and which makes Dart a refreshing attempt at change.

Yeah, yeah, the great new version of JavaScript will be here next year, same
as it has been going to be here next year for the past 10 years.

When it comes to making real progress you almost always need to buck the
standards and drag the world kicking and screaming into a better future and
then let the standards bodies figure out how to fit things in to their world
later on. I'm continually shocked there are so many people in this industry
that don't see this even though the proof for it is everywhere.

I'm not sure Dart really is the better future we need in a client-side
language as there is still a ton more work to be done (the technology is far
less baked than I expected it would be, even though it is only a preview
release), but I applaud Google for trying and I'd suggest other players trying
to sabotage their efforts should redirect their own efforts into something
even better than Dart. Win on the merits of the technology, not on slavish
devotion to existing standards. With increased competition everybody wins,
especially if everyone uses an OSS model as permissive as Google is with Dart,
and maintains JavaScript as a legacy lingua franca.

~~~
bzbarsky
> exactly the sort of morass that JavaScript has been > stuck in

You mean the one where some large corporations are doing what they can to keep
it from improving?

Seriously, Google has been sabotaging all sorts of JavaScript improvements by
flat-out refusing to implement them.

So in fact the "other players" _are_ directing their effort to something they
perceive as better than Dart: Harmony.

As for the rest, what will "win" is not necessarily going to be based on the
merits of the technology but on the strength of single-vendor tie-ins, in the
usual way....

~~~
georgemcbay
JavaScript was stuck in the mud before Chrome or V8 even existed. Before
Google, Microsoft and Yahoo got the blame.

Regardless of who the actors are that are most currently gumming up the works,
all of this just proves the point that standard bodies are not an effective
tool for actually creating useful technology.

~~~
bzbarsky
There was all sort of innovation in JavaScript going on at the time; still is.

As you note, getting the innovation into a standard is the hard part, when
some of the parties to the standards process don't actually want the language
to improve...

But Google claiming this is a problem for JavaScript while it is one of those
exact parties is just hypocritical.

------
yaakov34
This is called "a runtime". A bunch of things that have to be loaded on the
target machine for use by a compiled program. Just about every compiler works
this way, whether the output is machine code or JavaScript. It is possible to
not emit or load the parts of the runtime which will not be used by a given
program, but that's difficult and in many cases even impossible.

I am not a web programmer, and I don't use JS and don't plan to use Dart, but
I am flabbergasted that a bunch of techies are going wild and posting facepalm
pictures over a perfectly normal technical situation. Maybe Google will pare
down the runtime in the future, maybe they will figure out a way to exclude
parts of it which are not needed, but right now they are just making the
default technical choice that just about all compilers make. This is a non-
story. Compile "Hello world" into an executable, and see how many kilobytes it
takes on most architectures, despite the object file being a few bytes. Geez.

~~~
ootachi
In general it's true that you can't avoid the runtime, but in this case I
think people's expectations are colored by CoffeeScript, which offers (for
many) a more productive environment than vanilla JavaScript, with no runtime
at all. Since Dart is a CoffeeScript competitor in the "languages that compile
to JS" space, this is a legitimate disadvantage. Dart may well have so many
advantages over CS that this becomes a non-issue in practice, but we shouldn't
pretend this isn't a story at all.

~~~
yaakov34
I agree that it's perfectly legitimate to discuss the runtime needed for Dart,
and point out that it's a disadvantage that it is needed at all, or to
criticize the runtime, or whatever.

What shocks me is that people who are supposed to be techies are pointing
their fingers at the runtime and making monkey noises, because they apparently
have never seen a language runtime or heard of the concept. Dart did not
compile "Hello, world" to 17000 lines of code; most of that code has nothing
to do with the specific program. And that's the usual situation for compilers,
although some specific compilers can avoid that (e.g. CoffeeScript, which is a
close mapping of its target architecture).

~~~
maratd
The reason we are making monkey noises and pointing fingers is because we
expected a better JavaScript, not a Java that compiles to JavaScript. We
already have that. It's called GWT and this is just GWT 2.0 with a pretty
name.

~~~
stickfigure
There's already a GWT 2.0; this is more like GWT 9.0. And as someone who has
done a lot of work in GWT (and JS, and CS), I can see a lot of potential in
Dart. GWT is superior to javascript/coffeescript for a certain set of problems
(mostly fat-client-type webapps); a major upgrade to the language and tooling
would be welcome.

------
drgath
Funny. But to be fair, a Hello World in C compiles to a 27k binary. Yeah yeah,
optimize this, leave out that and you can get it smaller. Still doesn't change
the fact that Hello World is not the target use-case for either C or Dart.
Dart is intended for very large applications (compared to JS), so the
additional weight for includes isn't that big of a deal.

It also doesn't change the fact that Dart sucks. For other reasons though.

~~~
wes-exp
On 32-bit Linux GCC, C hello world comes out to only 7 KB. Without
optimization.

~~~
mkopinsky
Just as a datapoint (I did this test before seeing the comment), in 32-bit
cygwin a hello world written in C compiled to 12KB. In C++, it compiled to
497KB.

------
swannodette
I'll repeat what I said in the GitHub thread:

There's this interesting tool called Google Closure Compiler. It does dead
code elimination. <https://gist.github.com/1277285>

Notice that this source is now 2000 lines of _pretty printed_ JS. Given that
anything interesting these days uses jQuery or Underscore.js clientside, and
similar things serverside, this amount of JS for even Hello World is standard
fare.

EDIT: To be clear, I don't really give two hoots about Dart ... yet. I use
ClojureScript and since ClojureScript ships with a similarly rich API / data
structure set, Google Closure Compiler is essential to keeping code size down.

~~~
geoffhill
I'm sorry, but 2,300 lines of code is in no way acceptable as a "dead
code"-eliminated Hello World program. Not by a longshot.

~~~
tolmasky
If all you use javascript for is console.log("Hello World"); then v8 will seem
unnecessarily large too.

If all you do is one DOM transformation then jquery will seem huge compared to
just doing it by hand (probably hundreds or thousands of lines of code vs 3).

I guess at this point I'm just repeating the OP, but this is simply in no way
demonstrative of Dart. It can at best serve as a learning tool of the
internals (and perhaps there are valid criticisms once you look at those), but
any conclusion on code size is very flawed from this one example.

~~~
bad_user
jQuery is huge compared to doing it by hand, and I actually chose against
jQuery for a web service targeted at mobile phones.

However, in the case of jQuery the productivity / KB ratio is freakishly huge.

I fail to see how that will be the case for a new programming language.
Javascript is already fast and still improving in bounds and leaps. It is also
finally possible to write correct cross-browser code with the help of jQuery
and CoffeeScript. It is a true standard.

What can Dart really provide that Javascript cannot?

Refactoring tools? Some people would like that, but it still won't be bullet
proof (as I understand the static types in Dart are optional), and there's
nothing worse that a tool on which you can't rely on (personally I hate the
Intellisense attempts being made in IDEs for Ruby or Python).

What about speed? Well, Java/Quake2 was compiled to Javascript using GWT and
it worked just fine -- what more could you want from a sandboxed environment?
To me, these days big page load times either on my desktop or on my mobile are
almost always related to bandwidth.

------
josephg
I compiled that code with the google closure compiler. I used advanced
optimisations for dead code removal.

The result is 94% smaller - 32k, or 9k gzipped. A 32k overhead could be
absorbed by most large web applications without being noticed.

[http://closure-
compiler.appspot.com/code/jscd4ddca10c0a518fc...](http://closure-
compiler.appspot.com/code/jscd4ddca10c0a518fcb6ea6f56676ef790/default.js)

------
cubicle67
same code in coffeescript (@ preceding testMain makes it a static method and
it looks like print in Dart converts to console.log)

    
    
        class HelloCoffeeTest
          @testMain: -> 
            console.log "Hello, Coffee!"
        
        HelloCoffeeTest.testMain()
    

resulting js

    
    
        var HelloCoffeeTest;
        HelloCoffeeTest = (function() {
          function HelloCoffeeTest() {}
          HelloCoffeeTest.testMain = function() {
            return console.log("Hello, Coffee!");
          };
          return HelloCoffeeTest;
        })();
    
        HelloCoffeeTest.testMain();

~~~
rayiner
CoffeeScript is meant to be synaptic sugar on top of JS. Dart is a language
with different semantics that happens to compile to JS. As such it includes a
lot of runtime code. More appropriate examples would be Emscripten,
Parenscript, or ClojureScript.

~~~
ootachi
There _is_ a legitimate point here though. If I'm a developer working on a new
project, and I'm dissatisfied with JavaScript, I have a couple of options. I
could try Dart, which will cost me a significant amount of performance in all
browsers that aren't Chrome, or I could go with CoffeeScript, which will have
good performance in all browsers (including Chrome, since Chrome will be
forced by the other browser manufacturers to keep its engine up-to-date). For
me that'd be an easy choice, unless one of three things happens:

(1) Dart's performance is _so much_ better than that of JavaScript that it's
worth sacrificing performance in every other browser to get hugely better
performance in Chrome. That seems unlikely to me, but maybe it's possible.

(2) Chrome ends up with 90%+ market share, so it doesn't matter that I'm bad
in all the other browsers. But I can't see this happening as long as e.g. iOS
is around and relevant.

(3) I have the resources to write both a Dart version for Chrome and a
CoffeeScript version for older browsers. At this point, Google has strictly
made my life harder than it would have been otherwise.

I do want to give Dart a fair shake, but I'm having a hard time seeing how it
could realistically succeed.

~~~
swannodette
Anything happening in a client browser will involve a significant amount of IO
- querying / modifying DOM and remote requests. I don't think that choosing
Dart over CoffeeScript is going to make much of a difference for most
applications.

See Objective-J / Cappuccino which chose to eat the very real overhead
messaging in order to gain expressiveness. How many JS apps are as responsive
/ elegant as 280 North Slides?

~~~
ootachi
I'm referring mainly to the page load time increase, which is important,
especially on mobile. (Given what I've seen of the generated code, I suspect
runtime performance will be affected too, but I haven't tested, so I can't say
for sure.) This is why people create microframeworks—the page load time
improvement with e.g. zepto over jQuery is not insignificant.

------
beatpanda
I don't understand why Dart is supposed to be good, someone please explain it
to me like I'm an eight-year-old

~~~
KeithMajhor
It's not right now. It's a lousy CoffeeScript. The thing that makes dart good
is that it could, some day, have it's own runtime. Also, the language is being
designed by the team that did v8. So I'd imagine they plan to build support
into chrome at some point.

~~~
rayiner
I agree CoffeeScript has nicer syntax, but it retains all of Javascript's
semantic warts. Whatever quibbles you might have about Dart's syntax (it's
young yet!) it's semantics are much nicer than Javascript.

~~~
ootachi
Well, except for the unsound covariant generics, the lack of an arguments
object, and the implicit downcasts, and so on...

~~~
rayiner
Hint: Dart is a dynamically typed language. The covariant generics and the
implicit downcasts are there on purpose.

The lack of an arguments object is a feature not a bug. A rest argument like
in Lisp is much better for optimization. The design isn't done yet given that
Dart has optional arguments a rest argument would fit in.

------
pmr_
This reminds me of the very early days of C++ were people would use the size
of a compiled "Hello World" to argue their point. C++ targets a completely
different domain from JS where the size of the runtime etc. is somewhat
important and it makes sense to think about this.

I always thought that JS had this culture of "it works and is fast enough for
most users" attached to it, so I'm curious what is so different here? Every JS
application nowadays comes with tons of framework code already. Does it make a
big difference if you carry around a (non-minified) 17000 lines runtime?

Of course, if you argue about elegance, none of this matters.

------
mhansen
While this is hilarious, it's exactly fair. Dart is still in a very early tech
preview, and they clearly haven't optimised code generation yet.

Right now, it's probably designed to be run through something like Closure
Compiler, which removes dead code and minimizes the output.

------
InclinedPlane
Oooooooo. So it's Javascript except with a highly constraining layer of blub
on top of it. I'll go back to learning coffeescript then...

~~~
geon
I'd say the gist demonstrates that it's _not_ Javascript. What you see is the
entire runtime, compiled to naive js.

If anything, Coffeescript is "just" js with syntactic sugar. Not that there is
anything wrong with that.

~~~
cubicle67
that's a really interesting comment. where exactly is the line drawn between
"just syntactic sugar" and "a different language"?

Here we have two ways of expressing code, coffeescript and dart. Both are
syntactically quite different from js, both offer features not found in js,
both encourage you to write code differently to how you would in js and both
compile to plain js.

~~~
geon
> where exactly is the line

Not that I have an aswer to that, but I have the impression Coffeescript was
designed specifically to compile 1:1 to matching js.

A lot of the distinction seems to depend on the local culture of the language.
I've seen a lot of lispers talking about their new "language", where they add
some minor feature to scheme.

~~~
cubicle67
_...Coffeescript was designed specifically to compile 1:1 to matching js_

possibly initially, but there's a lot of constructs in coffeescript that
aren't in js eg existential operator

    
    
        solipsism = true if mind? and not world?
    

compiles to

    
    
        var solipsism;
        if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
          solipsism = true;
        }
    

see also splats <http://jashkenas.github.com/coffee-script/#splats>, function
binding and comprehensions over collections eg

    
    
        eat food for food in ['toast', 'cheese', 'wine']

------
perlgeek
So this includes the full Dart runtime library compiled to JS, and people make
a big deal out of it being a long listing.

------
jronkone
It's official now: Dart is meant as a practical joke to show why Javascript
needs to be replaced by something sane like a bytecode VM.

Either that, or I don't want to surf this internet anymore.

~~~
bjacobso
"Javascript needs to be replaced by something sane" really? You should read
what the goals are for Dart.

------
eddieplan9
I think the problem of Dart and Google's many strategies in web application
developer tool area is the belief that the current state of web app
development is in a very serious terminal illness that requires chemotherapy,
with a complete change of how we do things, while many others - notably the
CoffeeScript camp - have shown that we are still essentially in good shape and
all we need is some herbal tea to help the immune system. I am not discounting
the value of the bold move of Google, but I am very disappointed to see a big
negligence of the good parts of JavaScript and other dynamic languages and a
blind copy of Java (mis)features. I have not seen Google contributing to the
common and popular JavaScript frameworks like jQuery (and Microsoft did...
what a strange world). Maybe it's NIH syndrome at play.

------
vrotaru
They obviously embed into the emitted javascript the whole Dart runtime.
Whether it is needed or not.

------
carsongross
Wait, is this an indictment of Dart? Or of Javascript?

~~~
awj
I thought it was an indictment of ridiculous comparisons...

~~~
KirinDave
As you gaze into this thread, it gazes into you.

------
retrofit_brain
This comparison is asinine at best. The runtime is bound to be bigger. And
remember DART eventually will have the runtime included in Chrome and
hopefully other browsers. The way things are going in the browser world, all
people might actually care about is Chrome and if there is a wonderful IDE
support which comes with strongly typed languages, there is a real shot at
this becoming a standard.

------
tiles
I wonder if the only speed benefits that the Dart VM gives would be able to be
beaten out by a smart compiler. I know this is early for the cross-compilation
code (it doesn't trim out anything it doesn't need here), but I hope this
isn't what is ultimately used in that comparison.

~~~
josephg
As I understand it, dart isn't trying to be a faster javascript. Its trying to
be a better GWT.

Many googlers I've spoken to firmly believe that you need a strongly typed, OO
language with good tooling to be able to develop any large application. They
believe web apps are no exception.

Google has worked hard adding types to javascript with the closure compiler.
But, there are limits to what you can achieve using javascript.

Comfortable programming in the large is the main value proposition of Dart.
Expect to see good developer tools, debuggers, profilers and server-side
runtime environments for Dart appearing over the next few years.

(Disclaimer: I don't share this view)

~~~
gruseom
_dart isn't trying to be a faster javascript._

It isn't trying to be a javascript, but it's certainly trying to be faster.
Performance features prominently in every statement of their design goals. Not
only that, but performance in all modern browsers - i.e. including the ones
they don't control and won't ship with a Dart VM. I don't see how they can
possibly pull that off. Can anybody?

------
devin
The comments on this thread are a disgrace. Read more.

------
robinduckett
I'm noticing all those functions being defined with ever increasing argument
counts. I'm sure Google will optimise the output one day, as soon as they
learn about arguments.length.

------
V1
Google Dart was designed to build large scale web applications, yet all their
demos and example code only includes small applications. That still require
quite a lot of code to build.

I don't see any profit of building a application in dart, if you have to
create more code your self than you would with a regular HTML5 based
application.

As this "Hello World" demo shows, the only thing It's good at is creating
bloat and this will probably stay this way until native parsers are
implemented.

------
korny
Wow. Reminds me of GWT...

~~~
chii
you say that like GWT is somehow bad!

I think GWT is a great piece of tech. To be honest, google has way too much
NiH syndrome, even internally...

~~~
uriel
I still have trouble understanding how anyone can take something like GWT
seriously.

Whole books could be written about everything that is wrong with GWT, but here
is one post that barely scratches the surface:
[http://ryandoherty.net/2007/04/29/why-google-web-toolkit-
rot...](http://ryandoherty.net/2007/04/29/why-google-web-toolkit-rots-your-
brain/)

------
thomasfl
I prefer coffescript <https://gist.github.com/1277567>

------
jroseattle
If Microsoft had suggested this, they would be persecuted and beat into
submission.

~~~
Tloewald
Today yes. In 1995 it would get 95% market share.

~~~
jroseattle
Heh, true that.

------
stephenr
So, something that lets people who don't understand JavaScript "compile" some
other language into JavaScript, is horribly inefficient. Wow, what a surprise.

~~~
Ralith
Do you mean to imply that the point of Dart is to be an easier to understand
Javascript?

~~~
stephenr
Not at all. I'm saying if you want to make web apps that use JavaScript, write
JavaScript.

------
charliesome
People got just as pissed off as this over the amount of code Fructose (my
Ruby-PHP compiler) outputted for Hello World.

Looks like most here don't know how compilers work.

------
rhizome31
I haven't seen such a neatly formatted piece of JavaScript code in a long
time.

------
threedot
so fuckin what

------
dbbo
I can't believe that a gist somehow turned into a 4chan thread. I found that
infinitely more horrifying than the fact that a hello-world program nearly
crashed my browser through sheer size.

~~~
tripzilch
This is not going to be your century :-D

------
illumen
ewwwww

