Step-by-step guide to run ML models in WebAssembly. Achieve seamless AI integration in your web apps.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Example: A simple C++ function for inference (e.g., a linear regression)
// Compile this with Emscripten to generate a .wasm file.
#include
// Suppose this function computes a simple weighted sum
extern "C" {
// Use EMSCRIPTEN\_KEEPALIVE to ensure the function is not optimized-out
EMSCRIPTEN\_KEEPALIVE
float predict(float input, float weight, float bias) {
return (input \* weight) + bias; // Linear regression: y = wx + b
}
}
.wasm file, the next step is to load and instantiate it in your JavaScript environment. The browser’s WebAssembly.instantiateStreaming API allows you to instantiate the module directly from the compiled binary.
// JavaScript code to fetch and instantiate the WebAssembly module
// Fetch and instantiate the WASM file
fetch('model.wasm')
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {}))
.then(wasmModule => {
// The exported functions are available under wasmModule.instance.exports
const { predict } = wasmModule.instance.exports;
// Prepare inputs for the prediction function
const input = 2.0;
const weight = 3.5;
const bias = 1.0;
// Call the ML inference function from WebAssembly
const result = predict(input, weight, bias);
console.log('Prediction result:', result);
})
.catch(error => {
console.error('Error loading or running WASM module:', error);
});
Uint8Array or Float32Array for data exchange. When your inference function requires larger input data (e.g., image pixels), copy the data into the WASM module’s memory buffer before processing.
// Assume we have a function that expects an array of floats representing image data
// Allocate memory in the WASM module's memory
const numElements = 1024;
const bufferSize = numElements \* Float32Array.BYTES_PER_ELEMENT;
const ptr = wasmModule.instance.exports.malloc(bufferSize); // Custom exported malloc or equivalent
// Create a typed array view to the WASM memory
const wasmMemory = new Float32Array(wasmModule.instance.exports.memory.buffer, ptr, numElements);
// Copy data from JavaScript array to the WASM memory
const inputData = new Float32Array(numElements); // This would be your image pixel values or preprocessed data
// Populate inputData as required...
wasmMemory.set(inputData);
// Call the inference function that takes a pointer to the data
const inferenceResult = wasmModule.instance.exports.run\_inference(ptr, numElements);
// Free the allocated memory if necessary
wasmModule.instance.exports.free(ptr);
-O3 for high-level optimizations) when compiling your code.
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â