I’ve been working on an unofficial port of Elm to WebAssembly for a couple of years now. When I posted last year I had just managed to get my first compiled demo app working.
Well, as we all know, it’s a long way from a first demo of a system to actually getting things running smoothly! So that’s what I’ve been working on. It involved rewriting parts of the core libraries and garbage collector, and lots and lots of tests!
I have a few demos and a blog post to share.
The main one is the Elm SPA example running in WebAssembly. There’s also the same thing in JS for comparison. Quick performance measurements in Lighthouse give pretty similar results between the two at the moment, which is a great start! There are improvements coming.
It brings me joy to see progress is being made on wasm support for Elm. Keep up the good work!
One thing I didn’t see mentioned in the article, what is the compile time like? Does compiling to C and then wasm add significant overhead?
Also could this project serve as a stepping stone for running Elm on platforms outside the web? Or maybe the decisions made to get it interop with JS also make it difficult to use elsewhere?
Oh yes, that’s true, I forgot to mention compile times!
I ran a build of elm-spa-example just now, with an empty elm-stuff/. Elm to C took 1.7 seconds.
C to Wasm+JS took 5 seconds with full optimisations, or 3 seconds with no optimisations.
Yes it could serve as a stepping stone to other platforms, that’s one of the advantages of targeting C.
There would be a lot of other work involved of course. If you want Elm for command line apps or Elm for servers then you’d need to have good ideas for how to make those things nice, and then write libraries to implement that, and a lot of it would be in a low-level language like C.
But the JS stuff I’ve done wouldn’t get in the way much… I think! You’d probably need to add some kind of configuration options to turn things on or off, but that’s about it.
EDIT updated the compile times as I forgot I had another massive job running the first time!
Very interesting work!
One weird thing I have seen on the WebAssembly execution: in general, it turns out to be slower than javascript. Have you done any kind of targeted benchmarking to get an idea of runtime performance vs “traditional” js in some specific domains?
Eg: WebAssembly Zone - Daniel Lauzon - YouTube
Also I used your code as the basis for experimenting using Elm with multiple backend languages (eg rust, go), so: thank you!!