Working of Web-Assembly
Web-assembly is binary code or bytecode which does all the heavy lifting of logic and which can be used in your javascript.
Yes, browsers can understand the binary and since it’s bytecode, written with a statically typed language like Rust or C/C++, it can give you better performance.
I am no expert in Web-Assembly / WAMS, I am also learning, so in this article, I will be sharing a simple example which will help you understand the working of Web-Assembly and get started with it.
Let’s get started:
1. Generate the Bytecode or Binary Code:
To generate the WASM bytecode, I am using the following tool: https://wasdk.github.io/WasmFiddle/ which builds .wasm modules for you from a C-Program.
I am using the following simple c-program for calculating the factorial of a number.
A simple C-Program for calculating Factorial of a number.
Demo on WASDK website
Simply paste your C language code in the top-left section, click on build
, which will compile and build the WASM file or return an error if code has any syntax errors.
After building just click on the WASM Download icon to download the WASM file (bytecode / binary code) to be used in the Application.
2. Use the WASM file in your Javascript.
Most modern web-browsers execute web-assembly out of the box.
You just need to load the .wasm file and you can start executing functions from the .wasm file directly using Javascript.
Using the WASM file in your Javascript.
That’s all…
Thank you for reading.