Should Flutter web use Wasm instead of dart2js Should Flutter web use Wasm instead of dart2js dart dart

Should Flutter web use Wasm instead of dart2js


Vyacheslav Egorov (leading Dart AOT & JIT at Google) explains in this comment on Github that this is not clear at all that WASM is a better compilation target that JS.

It is without doubt that WASM is a better compilation target than JS for languages like C/C++ and Rust - languages where you manage your memory manually, where your calls are most often statically dispatched and primitive types are unboxed.

However if you start moving away from this kind of languages towards languages like Dart - which are full of dynamic behaviour, boxed primitive types and GC you will discover it becomes harder to claim with absolute certainty that WASM is a better compilation target than JS - because you would be forced to compile various runtime components (e.g. method dispatch implementation) into WASM - while in JS you get those things from underlying JS runtime and they are implemented natively and heavily optimised.

Another interesting thing to consider is that dart2js essentially benefits from two compilation steps - AOT compilation to JS and dynamic optimisation of this JS later by JS JIT. If AOT compiler fails to statically specialise some call site, there is still a chance that JS VM would manage to do that. You don't get such luxury with WASM.

There are a lot of other factors to consider (e.g. builtin libraries - do you want to implement your own array like structure in WASM with associated performance penalty, or do you just want to use heavily optimised native array?), etc, etc.

That said - I don't doubt that there are workloads and programs that would benefit from Dart targeting WASM. All I am saying is that expecting all Dart programs to magically get faster is incorrect.


During last Flutter Team AMA on reddit Flutter Team member Todd Volkert said:

WebAssembly is something we’re currently evaluating to see what opportunities exist for integrating it with Flutter. Nothing specific to report at this time.

Source


On July 27, 2021, Michael Thomsen (a Google product manager working on Dart and Flutter) published a blog post on the Dart blog titled, "Experimenting with Dart and Wasm". (Dart is the main programming language one uses when using Flutter to develop apps.)

They (Google) have been doing experiments to compile Dart to Wasm.

Bottom line: It's still early days, but it's definitely something they've been exploring.