November 23, 2026 · 10 min read
JavaScript has traditionally been the language of the browser. It has direct access to browser APIs, runs inside the browser's execution environment, and powers most interactive web applications.
But what if you want to run C++ inside a browser?
Imagine taking an existing C++ application — perhaps a PDF rendering engine, a chess engine, or a computationally intensive library — and making it run on the web without rewriting it in JavaScript.
This is possible because of WebAssembly (Wasm).
WebAssembly provides a compilation target that allows languages like C, C++, and Rust to run inside modern browsers. It brings a low-level, portable execution format to the web.
In this article, we'll follow the journey of a C++ program from source code to execution inside a browser. We'll explore the compiler toolchain, the role of Emscripten, JavaScript interoperability, memory management, and how the browser actually executes WebAssembly.
Let's start with a simple C++ program:
#include <iostream>
int main() {
std::cout << "Hello from C++!";
return 0;
}When we compile this program using a conventional native compiler:
g++ hello.cpp -o helloWe get a native executable.
That executable contains machine instructions targeting a particular processor architecture and operating system. For example, an executable compiled for x86-64 Linux is not directly executable on an ARM-based Mac.
Browsers, however, operate in a different environment.
They don't execute arbitrary native executables downloaded from websites. Doing so would introduce serious security risks and make applications dependent on the user's hardware and operating system.
We need something different: a portable, sandboxed execution format that browsers can understand.
That's where WebAssembly comes in.
WebAssembly is a binary instruction format designed as a compilation target for the web.
Unlike JavaScript, which is a high-level programming language, WebAssembly is a low-level format designed to be efficiently generated by compilers and executed by runtime environments.
A WebAssembly module is generally distributed as a .wasm binary.
Some important characteristics:
An important distinction: WebAssembly is not x86 machine code packaged differently. It is its own instruction set with a well-defined execution model. The browser's Wasm engine validates and compiles it into instructions suitable for the underlying machine.
How does our C++ program become a WebAssembly module?
This is where Emscripten and LLVM enter the picture.
Emscripten is a compiler toolchain that makes it possible to compile C and C++ applications to WebAssembly.
Under the hood, it uses Clang and LLVM.
The simplified compilation pipeline looks like this:
C++ Source Code
|
v
Clang
|
v
LLVM IR
|
v
WebAssembly Code
|
v
Linker
|
v
+------------------+
| hello.wasm |
| hello.js |
| hello.html |
+------------------+Let's understand these stages.
Clang is the C/C++ compiler frontend used by Emscripten.
It parses the C++ source code, performs semantic analysis, and converts the program into LLVM Intermediate Representation (LLVM IR).
LLVM IR is an intermediate representation that separates language-specific frontend concerns from architecture-specific code generation.
This separation is powerful.
The same C++ source code can be compiled to different targets without changing the source itself.
LLVM takes the intermediate representation and generates code for the selected target architecture.
In our case, the target is WebAssembly.
Instead of generating x86-64 or ARM64 instructions, LLVM generates WebAssembly instructions.
The linker combines compiled object files and required libraries into the final WebAssembly module.
This is also where Emscripten's runtime requirements become important.
For example, C++ applications may depend on functionality such as:
Some of these capabilities require runtime support or browser-compatible implementations.
Let's compile the earlier example:
em++ hello.cpp -o hello.htmlEmscripten typically generates three important files:
| File | Purpose |
|---|---|
hello.wasm | The compiled WebAssembly module |
hello.js | JavaScript runtime and integration glue |
hello.html | A convenient HTML shell for running the application |
The generated JavaScript is not simply an optional wrapper. Depending on the build configuration, it can provide important runtime functionality, initialize the Wasm module, and connect the application to browser APIs.
Now we have our WebAssembly module. How does the browser execute it?
When a browser loads an application built with Emscripten, several things happen.
The browser first loads the generated JavaScript file.
This JavaScript contains the integration logic required to initialize the application.
Depending on the generated output, it can also handle:
The JavaScript runtime loads the .wasm file, usually through fetch().
Browsers provide the WebAssembly JavaScript API to work with these modules.
For example:
const response = await fetch("hello.wasm");
const result = await WebAssembly.instantiateStreaming(
response,
imports
);
const instance = result.instance;This is a simplified example of loading a Wasm module directly. Emscripten-generated applications generally use their own generated initialization machinery.
Before execution, the browser validates the WebAssembly module.
The Wasm engine checks that the module follows the WebAssembly specification, including its type and instruction constraints.
The engine then compiles the module into code that can execute on the host machine.
Modern browsers use optimizing compilation strategies, and may use different compilation tiers to balance startup time and peak performance.
After compilation, the module is instantiated.
Instantiation connects the module with its imported resources, such as:
The resulting instance exposes exported functions and other resources.
The browser can now execute the WebAssembly code.
A browser application rarely consists of WebAssembly alone.
JavaScript continues to play an important role because it has direct access to browser APIs such as:
WebAssembly handles computationally intensive logic, while JavaScript often handles browser integration.
But how do the two communicate?
Consider a C++ function:
extern "C" {
int add(int a, int b) {
return a + b;
}
}We can export this function through Emscripten:
em++ hello.cpp \
-O2 \
-s EXPORTED_FUNCTIONS='["_add"]' \
-o hello.jsWith the appropriate Emscripten module initialization, JavaScript can call the exported function:
const result = Module._add(10, 20);
console.log(result); // 30The extern "C" declaration prevents C++ name mangling, making the function easier to expose through a C-compatible interface.
Emscripten also provides higher-level interoperability mechanisms such as ccall and cwrap.
For more complex applications, understanding how data moves between JavaScript and WebAssembly becomes especially important.
One of the most interesting aspects of WebAssembly is its memory model.
Unlike a native C++ application, which operates within the process's virtual address space, a WebAssembly module uses a linear memory abstraction.
Linear memory is a contiguous, byte-addressable memory region exposed to the Wasm module.
Conceptually, it looks like this:
+--------------------------------------+
| |
| WebAssembly Memory |
| |
+--------------------------------------+
|
v
+--------------------------------------+
| |
| Linear Memory |
| |
| +--------------------------------+ |
| | Stack | |
| +--------------------------------+ |
| | | |
| | Heap | |
| | | |
| +--------------------------------+ |
| |
+--------------------------------------+The diagram is conceptual. The actual layout depends on the compiler, runtime, and application.
In a typical Emscripten application, C and C++ memory operations are implemented using the module's linear memory.
For example:
int* values = new int[100];
values[0] = 42;
delete[] values;The allocation is handled by the C++ runtime and allocator operating within the WebAssembly memory environment.
JavaScript can interact with the module's linear memory through the WebAssembly.Memory API.
For example:
const memory = new WebAssembly.Memory({
initial: 10,
maximum: 100
});The initial and maximum values are specified in WebAssembly pages. One page is 64 KiB.
This means that an initial memory size of 10 pages represents:
10 * 64 KiB = 640 KiBWebAssembly memory can grow when permitted by its limits and configuration.
However, memory growth has implications for JavaScript interoperability. For example, JavaScript typed-array views over the memory may need to be recreated after memory growth.
Understanding this becomes particularly important when working with large C++ applications.
Yes.
WebAssembly supports multithreading through shared memory and atomic instructions.
However, browsers introduce additional constraints because JavaScript execution environments are designed around browser security and responsiveness.
Emscripten implements C/C++ threading support using Web Workers and shared memory.
A simplified architecture looks like this:
Browser
|
Main JavaScript
|
WebAssembly
|
Shared WebAssembly Memory
/ \
/ \
Web Worker 1 Web Worker 2
| |
Wasm Thread Wasm ThreadWhen an application uses std::thread, Emscripten maps the threading functionality to browser-supported mechanisms.
Some important concepts include:
A multithreaded WebAssembly application therefore needs more than simply compiling with a threading flag.
For example:
em++ hello.cpp \
-pthread \
-sPTHREAD_POOL_SIZE=4 \
-o hello.htmlThe exact configuration depends on the application's requirements and deployment environment.
This is an interesting question, and the answer is: it depends.
WebAssembly is designed for efficient execution, but it doesn't automatically make every C++ application as fast as its native counterpart.
Performance depends on several factors:
For CPU-intensive workloads with relatively little interaction with JavaScript, WebAssembly can achieve performance close to native code.
However, applications that frequently cross the JavaScript-Wasm boundary or depend heavily on unsupported system functionality may encounter additional overhead.
The right approach is to measure real workloads rather than assume that one execution environment will always be faster.
We started with a simple C++ program and followed it through the compilation pipeline into the browser.
Along the way, we explored:
The interesting part is that WebAssembly isn't just about bringing C++ to the browser.
It creates an environment where applications traditionally associated with native systems can run in a browser while retaining much of their existing architecture and codebase.
This opens up possibilities for PDF engines, image processing libraries, games, scientific computing, CAD applications, and many other computationally intensive workloads.
And that's what makes WebAssembly such an interesting piece of technology: it brings systems programming much closer to the web.