15 ms·
"Hello World" in Dart, compiled to JavaScript
- stephenr 15y agoSo, something that lets people who don't understand JavaScript "compile" some other language into JavaScript, is horribly inefficient. Wow, what a surprise.
- beatpanda 15y agoI don't understand why Dart is supposed to be good, someone please explain it to me like I'm an eight-year-old
- KeithMajhor 15y agoIt'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 15y agoI 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 15y agoWell, except for the unsound covariant generics, the lack of an arguments object, and the implicit downcasts, and so on...
- rayiner 15y agoHint: 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.
- cubicle67 15y agoI agree CoffeeScript has nicer syntax, but it retains all of Javascript's semantic warts that comes across as both patronising and ignorant (although it could be me who's the latter). any examples of "all Javascript's semantic warts" coffeescript retains?
- masklinn 15y ago> I don't understand why Dart is supposed to be good It's like Go: "It's from google, so awesome by default".
- tiles 15y agoI 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.
- deleted 15y ago[deleted]
- spullara 15y agov8 is not crazy fast. It is a fast dynamic language VM -- beats php, ruby, python and other javascript VMs. In absolute terms, it executes code slowly compared to, for instance, the Java, Scala, C, C++, C# and others.
- ootachi 15y agoV8 does not use static type inference, as you seem to suggest. It uses dynamic type specialization based on observed types. Only recent builds of SpiderMonkey (not those that are shipping in current Firefoxes) do static type inference.
- josephg 15y agoAs 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 15y agodart 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?
- korny 15y agoWow. Reminds me of GWT...
- chii 15y agoyou 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 15y agoI 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-rots-your-brain/ http://ryandoherty.net/2007/04/29/why-google-web-toolkit-rot...
- mhansen 15y agoWhile 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.
- swannodette 15y agoI'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 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 15y agoI'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.
- swannodette 15y agoWhat language do you program in where the dependencies are not as heavy as they are here? The only difference I can tell is that it's hidden from you.
- rimantas 15y agoThe other difference is that they are not transferred over wire (once if developers were careful or on every request if they were not).
- tolmasky 15y agoIf 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.
- josephg 15y agoI 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/jscd4ddca10c0a518fcb6ea6f56676ef790/default.js http://closure-compiler.appspot.com/code/jscd4ddca10c0a518fc...
- vrotaru 15y agoThey obviously embed into the emitted javascript the whole Dart runtime. Whether it is needed or not.
- cubicle67 15y agosame 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();
- swannodette 15y agoDoes CoffeeScript have all these data structures, http://www.dartlang.org/docs/api/index.html http://www.dartlang.org/docs/api/index.html?
- DanielRibeiro 15y agoYou can always import JS.Class: http://jsclass.jcoglan.com/ http://jsclass.jcoglan.com/
- swannodette 15y agoWhich is > 300K before minification.
- cubicle67 15y agoThat's a long list, so probably not. Does Dart have an existential operator?[0] zip = lottery.drawWinner?().address?.zipcode Hey look, I know they're not really comparable. They're solving different problems, are at different stages in maturity and have a different philosophy on how to do things. I also think that Hello World is probably the worst way to compare any two languages, but it's a fun exercise anyway [0] example code from http://jashkenas.github.com/coffee-script http://jashkenas.github.com/coffee-script
- rayiner 15y agoCoffeeScript 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.
- drgath 15y agoFunny. 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.
- riya 15y agohbgh
- DasFuxx 15y agoCause you doing it wrong. As i already mentioned in the gist comments, putting the c library static into the application is like putting the browser into js. Without a lot of optimizing iam around 5k binary and 200 lines of assembler (without linker and other header overhead)
- wes-exp 15y agoOn 32-bit Linux GCC, C hello world comes out to only 7 KB. Without optimization.
- mkopinsky 15y agoJust 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.
- sjs 15y agoAs 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 15y ago..and here's the jsperf for the --optimize'd hello world vs. vanilla JS - http://jsperf.com/dart-hello-world-test-with-optimize/2 http://jsperf.com/dart-hello-world-test-with-optimize/2. 17k vs. 4M for me
- sjs 15y agoGot 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 15y agoDart 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
- carsongross 15y agoWait, is this an indictment of Dart? Or of Javascript?
- jronkone 15y agoIt'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 15y ago"Javascript needs to be replaced by something sane" really? You should read what the goals are for Dart.
- perlgeek 15y agoSo this includes the full Dart runtime library compiled to JS, and people make a big deal out of it being a long listing.
- devin 15y agoThe comments on this thread are a disgrace. Read more.
- KirinDave 15y agoSo 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 15y agoAmen. 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 15y agoSorry 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 15y agoThere 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 15y agoWouldn'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.
- dbbo 15y agoI 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 15y agoThis is not going to be your century :-D
- InclinedPlane 15y agoOooooooo. 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 15y agoI'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 15y agothat'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 15y ago> 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 15y ago...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 http://jashkenas.github.com/coffee-script/#splats, function binding and comprehensions over collections eg eat food for food in ['toast', 'cheese', 'wine']
- charliesome 15y agoPeople 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.
- illumen 15y agoewwwww
- thomasfl 15y agoI prefer coffescript https://gist.github.com/1277567 https://gist.github.com/1277567
- robinduckett 15y agoI'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.
- pmr_ 15y agoThis 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.
- V1 15y agoGoogle 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.
- rhizome31 15y agoI haven't seen such a neatly formatted piece of JavaScript code in a long time.
- yaakov34 15y agoThis 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 15y agoIn 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 15y agoI 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).
- retrofit_brain 15y agoThis 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.
- jroseattle 15y agoIf Microsoft had suggested this, they would be persecuted and beat into submission.
- Tloewald 15y agoToday yes. In 1995 it would get 95% market share.
- jroseattle 15y agoHeh, true that.
- deleted 15y ago[deleted]
- eddieplan9 15y agoI 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.
- threedot 15y agoso fuckin what