Node.js with WebAssembly

Introduction

WebAssembly is a high-performance assembly-like language. It can be compiled from C/C++, Rust, and AssemblyScript, among other languages. Right now, it is supported by Chrome, Safari, Edge, Firefox, and Node.js!

The WebAssembly specifies two file formats: a binary format known as a WebAssembly Module (.wasm) and a text representation known as WebAssembly Text (.wat).

      Source

Key Concepts

  • Module - A compiled WebAssembly binary, i.e., a .wasm file.
  • Memory - A resizable ArrayBuffer.
  • Instance -A Module's memory, table, and variables are instantiated.
  • Table - A resizable array of references.

Generating WebAssembly Modules

WebAssembly binary files can be generated using various methods like:

  • By writing WebAssembly (.wat) yourself and converting to binary format using tools such as wabt.
  • Using emscripten with a C++/C application.
  • Using wasm-pack with a Rust application
  • Using AssemblyScript if you prefer a TypeScript-like experience

Some of these tools produce the binary file, JavaScript "glue" code, and corresponding HTML files for browser execution.

 

Getting Started

Let us learn through a simple example by creating an additional function.

  • Install Rust and Cargo (package manager) with https://rustup.rs/
  • Install Wasm-Pack for compilation Rust code to WebAssembly with this
  • Generate a new project from boilerplate with:
wasm-pack new add
  • Build with:
cd ./add && wasm-pack build --target nodejs
  • The compilation result will be placed in the pkg directory by default.

 

Once you have added a WebAssembly module, you can now use the Node.js WebAssembly object to instantiate it.

 

// Assume add.wasm file exists that contains a single function adding two provided arguments
const fs = require('fs');
const wasmBuffer = fs.readFileSync('/path/to/add.wasm');
WebAssembly.instantiate(wasmBuffer).then(wasmModule => {
  // Exported function live under instance.exports
  const add = wasmModule.instance.exports.add;
  const sum = add(56);
  console.log(sum); // Outputs: 11
});

 

The output of the above code is 11.

WebAssembly modules are unable to access OS functions on their own. It can only be accessed through a third-party tool like Wasmtime. Wasmtime uses the WASI API to gain access to OS features.

Learn using WebAssembly in NodeJS with more examples. Click here to learn more.

After going through a few examples, try to compare the runtime performance of both WebAssembly and simple JAvascript. You will observe that WebAssembly is faster.

Frequently Asked Questions

Q1: What is WebAssembly language?

Ans: It is basically a low-level assembly-like language. It uses a small binary format that runs with near-native performance and provides languages like C++, C,  C#, and Rust with a compilation target to run them on the web.

 

Q2: What are the platforms that support Assembly Language.

Currently,  it is supported by Chrome, Firefox, Safari, Edge, and Node.js.

Q3: Can JavaScript compile to Wasm?

Ans: Yes, it is possible to call JavaScript functions from inside in your running WebAssembly functions.

 

Q4: What is a node web?

 

Ans: Node is an open-source, cross-platform runtime environment. It allows developers to create all kinds of server-side tools and applications in JavaScript. 

Key Takeaways

As you can see, WebAssembly is easy to use with Node.js and is faster. WebAssembly is a new technology and still in the experimenting stage, such as using WASI to access system services, but it has a lot of potentials.

 

Coding Ninjas provides a whole course on NodeJS. It provides basic to Advance level understanding. Enroll today and build amazing websites using Node. Click on the given link to enroll.

 

By- Gazal Arora

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think