Running Clojure in WASM Apr 26, 2025 Starting from v25 GraalVM added support for WASM backend for Java programs compiled to native image, which means that it's finally becomes possible to compile and run Clojure programs in WASM! VIDEO Although WASM backend is in its early days, and doesn't support threading and networking, it is already possible to compile and run single-threaded computational programs in Java/Clojure. If you open browser console now, on this page, you'll see Hello, World! printed in the console. That's a Clojure program talking to you :) (ns core (:gen-class)) (defn -main [& args] (println "Hello, World!")) Binary size The output WASM of this simple program is 5.6MB binary, which can be pruned a bit via wasm-opt tool, just make sure that it doesn't break anything for you. Luckily when compressed (gzip, brotli, etc) the binary becomes just ~2.5MB in size. After running the binary through wasm-opt I got 5MB output, so roughly ~600KB less ones and zeroes. wasm-opt core.js.wasm -o core.js.wasm -Oz --enable-gc --enable-strings --enable-reference-types --enable-exception-handling --enable-bulk-memory --enable-nontrapping-float-to-int In comparison, this basic hello world program in Java results in just 1MB of WASM. public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } To give you a sense of how binary size changes with added libraries, using clojure.data.json increases WASM binary by 130KB. Note that if a namespace is required but never used, it's pruned from the output. (ns core (:require [clojure.data.json :as json]) (:gen-class)) (defn -main [& args] (prn (json/write-str {:a 1 :b 2}))) For detailed analysis GraalVM provides a build report, that says that 70% of compiled output is heap snapshot and roughly 50% of it is filled with strings and hash maps. It's interesting that number of methods chanrt shows that majority, 60% of methods are coming from various Java namespaces, while Clojure only occupies 1...
First seen: 2025-04-28 21:21
Last seen: 2025-04-28 23:21