Dart2js producing large javascript files, even with little code?
Asked Answered
S

2

2

I'm new to dart, and am using Dart Editor on Windows.

I noticed that my compiled javascript is huge, so I kept removing more and more code to see what was causing it, but I don't think I'm getting anywhere. Here's my program right now:

import 'dart:html';

void main() {
  var video = querySelector("#vid");
}

That's literally it. I've stripped out everything but one instruction.

And this is the produced javascript (it won't fit inline):

https://gist.github.com/DSteve595/504887a19a05614bcc94

What am I doing wrong? This program's practically empty!

Smoothen answered 14/1, 2014 at 20:57 Comment(0)
G
6

The file you linked to in the gist is 48k. If you ran dart2js with the --minify option, you could bring the size down quite a bit.

Your code may be trivial, but dart2js has to load a significant number of libraries by default, and it has to load the dart:html library that you import as well. For perspective, how big do you think your file would be if you wrote your program in JavaScript and imported all of jQuery?

You can read more about dart2js at https://www.dartlang.org/docs/dart-up-and-running/contents/ch04-tools-dart2js.html.

Gondola answered 14/1, 2014 at 21:19 Comment(1)
You know, I hadn't even considered that JQuery is no longer being imported.. Maybe this isn't so bad.Smoothen
F
3

dart2js needs to generate a significant amount of code to emulate Dart behaviour in JavaScript. Types, error checking, math, etc. all need to be emulated in JavaScript. This results in a lot of extra code and a trivial "Hello World" app will look monstrously large.

You can reduce the size of the delivered JavaScript by minifying it and gzipping it.

dart2js has been getting better over time, but there will always be a size cost to emulating Dart behaviour in JavaScript.

Fatuitous answered 14/1, 2014 at 21:13 Comment(2)
Alright, so most of this is just boilerplate I can't get rid of? What about the frogc compiler that apparently used to be included with Dart Editor? It looks like the code produced by that was way more compact.Smoothen
Right. frog is an older compiler that is not supported, I wouldn't use it. Apparently frog didn't cover the whole Dart language, which may be why it was smaller.Fatuitous

© 2022 - 2024 — McMap. All rights reserved.