Lesson 18 min
WebAssembly & Native Compilation
00:00 / 00:00
Dart's Compilation Targets
Dart is one of the few languages that compiles to four distinct targets:
- Native ARM/x64 machine code (mobile, desktop apps via Flutter)
- JavaScript (web apps via
dart2jsor DDC) - WebAssembly (Wasm) (next-gen web, introduced in Dart 3.3)
- Kernel bytecode (for the Dart VM during development)
WebAssembly support in Dart is a game-changer. It means Flutter Web apps can run at near-native speed in the browser — no JavaScript overhead.
dart2js — Compiling Dart to JavaScript
The dart2js compiler is a whole-program optimizing compiler:
- Performs tree-shaking — only includes code that's actually used
- Type inference — uses Dart's type information to generate tighter JS
- Minification — renames identifiers to minimize bundle size
- Output is standard ES5/ES6 JavaScript, compatible with all browsers
bash# Compile a Dart app to optimized JavaScript dart compile js -O2 -o output.js bin/main.dart # O2 = optimized mode (tree-shaken, minified)
WebAssembly — The Future of Dart on the Web
Flutter Web now supports a --wasm compilation target:
bash# Build Flutter web app targeting WebAssembly flutter build web --wasm
Why this matters:
- 2-3x faster rendering compared to JavaScript output
- Runs in a sandboxed, memory-safe environment
- Predictable performance (no JS GC pauses)
- Works in Chrome, Firefox, Edge, and Safari (with flags)
Native Compilation
For non-Flutter Dart programs (CLI tools, servers):
bash# Compile a Dart script to a self-contained native binary dart compile exe bin/server.dart -o server # The output is a standalone executable — no Dart SDK required ./server
dartimport 'dart:io'; void main() async { final server = await HttpServer.bind('localhost', 8080); print('Server running on :8080'); await for (final request in server) { request.response ..write('Hello from Dart Native!') ..close(); } }
Summary
Dart's multi-target compilation story is genuinely impressive. The same language and same codebase can produce a native iOS app, an Android app, a desktop executable, a JavaScript bundle, and a WebAssembly module. This is the technical foundation that makes Flutter's "write once, run anywhere" promise real.