I Shrunk my Dart-to-JS code by 11X and So Can You

Dart, the structured and scalable programming language for web apps, compiles to JavaScript thanks to dart2js. This means Dart apps run across the modern web. When you compile to JavaScript, you want to ensure dart2js generates the smallest amount of bytes. Less bytes means smaller bandwidth bills, and faster load times, and longer battery life. All good things! Read on to learn how to minify your generated JavaScript.

For this example, I will use the offline-enabled Todo app that I built to highlight Lawndart, my easy library for browser storage. The Todo app uses quite a few libraries:
  • dart:html
  • Lawndart
  • WebSQL
  • IndexedDB
  • Local storage
  • Web UI
The original generated JavaScript output size was 612,249 bytes. Minified, it became 289,840 bytes. Minified and gzipped it became 56,496 bytes. That's about 11X smaller than the original output!

(Measuring the original size of the code is tricky. Do I count the lines that I wrote? Do I count the lines generated by the Web UI compiler? I count 9,313 bytes of generated Dart code from the Web UI compiler, which is the code that would be shipped down to Dartium during development. This does not count the size of the packages required. Also, most developers would run a tree-shaking tool on the Dart code if they want to ship pure Dart in production. When I ran dart2dart to generate a single file that contained all the Dart code required for the app, I ended up with 44,860 bytes unminified and ungzipped.)

Let's learn how to get compiled JavaScript output as small as possible.

Step One: Turn on Minification

The editor does not ship with minification turned on by default. Say what?! Fear not, we can fix that.

  1. Right click on your project
  2. Select properties
  3. Select Dart Settings
  4. Add --minify to "Additional flags:"
  5. Embrace the smallification
See this in action:



(Here's a bug that requests to make this easier, please star!)

If you don't use Dart Editor, you can also minify from the command line. Here is an example:

dart2js --minify --out=app.js app.dart

The file shrinks to 289,840 bytes with minification, a 2.1X shrinkage!

Step Two: Compress the file on-the-fly

Most web servers will compress your files for you, on the fly! See your web server's configuration for more details.

The file shrinks to 56,496 bytes with gzip, a 11X shrinkage from the original!

Step Three: There is no step three!

Yup, just use --minify with dart2js and be sure your server has turned on compression/gzip.

To be sure, we're not yet happy with the output size and we're working hard to generate even smaller code. However, minification really helps and you should use it today. Please file bugs and feature requires at dartbug.com. Thanks!
3 comments

Popular posts from this blog

  • Sponsor:  Register today for  New Game, the conference for HTML5 game developers . Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.  Register now ! This is the second article in a Box2D series, following the Box2D Orientation article. The Box2DWeb port of Box2D contains a nice example to show off the basics of integrating physics simulations into your web app. This post will provide a walkthrough of the example, explaining the high level concepts and code. First, let's see the example in action. The code for the above is open source and available on GitHub. It was adapted from Box2DWeb's example . Animating Before we look at Box2D, it's important to understand how the above simulation is animated. You might think setInterval or setTimeout is
    Keep reading
  • In which I port a snazzy little JavaScript audio web app to Dart , discover a bug, and high-five type annotations. Here's what I learned. [As it says in the header of this blog, I'm a seasoned Dart developer. However, I certainly don't write Dart every day (I wish!). Don't interpret this post as "Hi, I'm new to Dart". Instead, interpret this post as "I'm applying what I've been documenting."] This post analyzes two versions of the same app, both the original (JavaScript) version and the Dart version. The original version is a proxy for any small JavaScript app, there's nothing particularly special about the original version, which is why it made for a good example. This post discusses the differences between the two implementations: file organization, dependencies and modules, shims, classes, type annotations, event handling, calling multiple methods, asynchronous programming, animation, and interop with JavaScript libraries. F
    Keep reading
  • Angular and Polymer, sitting in a DOM tree, B-i-n-d-i-n-g. First comes components, Then comes elements, Then comes the interop with the node dot bind. Angular , a super heroic MVC framework, and Polymer , polyfills and enhancements for custom elements built on top of Web Components, can live harmoniously in the same app. This post shows you how to connect Angular-controlled components to Polymer-controlled elements via data binding. And we do it all in Dart . Angular and Polymer I get asked "Should I use Angular or Polymer?" a lot. My answer is, "Yes". That is, both libraries have distinct strengths, and you can use both in the same app. Polymer excels at creating encapsulated custom elements. You can use those custom elements in any web app or web page, regardless if that app is built with Angular, Ember, etc. Angular excels at application engineering, with dependency injection, end-to-end testability, routing, and services. Here are som
    Keep reading