One function, many languages: A WebAssembly experiment

Posted By
Dhruvisha Bansal

A few weeks ago, our team got curious about WebAssembly (WASM) and its promise of running code at near-native speeds in the browser. We set out to test how each WASM language performs for the same mathematical function when compiled to WebAssembly code. This WASM browser experiment led us to build a small web app to compare WASM performance across Rust, Go, C, and Python, giving us hands-on insights into WebAssembly’s potential.
What is WebAssembly?
WebAssembly is a binary format that lets WebAssembly code written in various WASM languages run efficiently in a WebAssembly browser. It compiles to near-machine code for fast execution, outpacing JavaScript in compute-heavy tasks. Its sandboxed WASM runtime keeps things secure, and its portability means code can work across platforms.
We wanted to see how WebAssembly handled a specific math function and whether WASM compilation varied by language.
The WASM experiment: Same function, different languages
For our WASM browser experiment, we built a web app to compute nⁿ (n raised to the power of n) across a range of numbers, like 50 to 20,000. This math-intensive function was perfect for testing WASM performance. Users could pick a range, select a WASM language (Rust, Go, C, or Python), and see how long the WebAssembly code took to run. Our goal with this perfect CPU-heavy task was to compare how each language performed after WASM compilation.
Problem statement
We had two questions:
- Can WebAssembly compute nⁿ efficiently enough for a responsive WebAssembly browser experience?
- Does the choice of WASM languages affect WASM performance in this WebAssembly performance comparison?
We built a small web app where you:
- Select a range of numbers (e.g., 50 to 20000)
- Choose a language – Rust, Go, C, or Python
- Compute nⁿ for each number in that range
- See the execution time for each version
Why WebAssembly?
WebAssembly makes it possible to run compiled languages in the browser. This means:
- Developers can reuse system-level code on the web.
- Apps can gain performance boosts where JavaScript might struggle.
- Developers aren’t locked into one language—they can choose the best tool for the job.
But does compiling to WASM level the playing field, or do some languages still outperform others?
Languages we tested
We coded the power(base, exponent) function in four WASM languages, each with distinct strengths:
- Rust – Fast, safe, and ideal for systems programming
- Go – Simple and efficient, with TinyGo for WASM
- C – The OG speedster, compiled with Emscripten
- Python – via Pyodide, bringing the popular scripting language to the browser
How it works under the hood
Each language implements the same power() function. We compile each to .wasm using its ecosystem tools. Then the frontend loads and runs them on user input.
Here’s a breakdown of the workflow:
1. Define the power function
Rust:
#[wasm_bindgen] pub fn power(base: f64, exponent: f64) -> f64 { base.powf(exponent) }
C:
double power(double base, double exponent) { return pow(base, exponent); }
Go:
func power(base float64, exponent float64) float64 { return math.Pow(base, exponent) }
Python (Pyodide):
def power(base, exponent): return math.pow(base, exponent)
2. Compile to .wasm
Language | Tool Used |
---|---|
Rust | wasm-pack |
Go | TinyGo |
C | Emscripten |
Python | Pyodide (precompiled runtime) |
3. Load and run in browser
JavaScript handles runtime loading and calculates execution time for each language:
let start = performance.now(); for (let i = from; i <= to; i++) { result = wasmModule.power(i, exponent); } let end = performance.now(); console.log(`Execution time: ${end - start} ms`);
The results (Try it yourself!)
The beauty of this project is you can run the test yourself in your browser. Every language has strengths, and performance may vary based on:
- How the WASM module was compiled
- Overhead from bridging between JavaScript ↔ WASM
- The language's own runtime or memory model
Work with Opcito’s WASM Experts
At Opcito, our team of WebAssembly experts is ready to help you harness WASM’s power for your next web application. Whether you’re exploring WASM languages, optimizing WebAssembly code, or building high-performance browser solutions, we have the expertise to guide you. Contact us to learn more about WebAssembly or discuss your WASM browser experiment needs.
Related Blogs
