Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Key Concepts
3.
Generating WebAssembly Modules
4.
Getting Started
5.
Frequently Asked Questions
6.
Q1: What is WebAssembly language?
7.
Key Takeaways
Last Updated: Mar 27, 2024

Node.js with WebAssembly

Author GAZAL ARORA
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Also see, Difference Between Controller and Restcontroller

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

 

Previous article
How to Read Environment Variables from Node.js
Next article
NPX : Node.js Package Runner
Live masterclass