WebAssembly for Node.js (update April 2023)

Alex Vasilev
Geek Culture
Published in
4 min readJun 21, 2021

--

Everybody talks about WebAssembly and how it will change the web. But what about Node.js? Can WebAssembly be useful for backend? Let’s figure it out!

Node.js runs on top of V8, the same JavaScript engine is used in Google Chrome. Google Chrome has supported WebAssembly since the 57th version. It means that it’s possible to run WebAssembly inside Node.js

Our plan for today:

  1. Write some simple function. For example calculation of fibonacci sequence.
  2. Compile it ot WebAssembly
  3. Import it to Node.js
  4. Compare results with pure JavaScript code

Choosing programming language for WebAssembly

First of all we need to choose a programming language with WebAssembly support. You can find the full list with notes here: https://github.com/appcypher/awesome-wasm-langs. My first try was Golang, compilation of Go code to WebAssembly is very simple:

$ GOOS=js GOARCH=wasm go build -o main.wasm

But there is a problem, the only option to import function from Go WebAssembly to JavaScript is setting it to the global object with this code:

js.Global().Set("myFunc", fn)

--

--