# WebAssembly vs Container vs MicroVM for LLM-Generated Code

> Use Wasm for small pure-Python snippets. It is light and can only touch the directories you grant, but C extensions, pip builds and raw sockets mostly fail. A container runs anything but shares the host's kernel. A microVM gives the code its own kernel and a full Linux machine, at the cost of a whole VM.

Source: https://sandboxer.com/guides/wasm-vs-container-vs-microvm-for-llm-generated-code. Updated 2026-11-20.

**Short answer:** Use Wasm for small pure-Python snippets. It is light and can only touch the directories you grant, but C extensions, pip builds and raw sockets mostly fail. A container runs anything but shares the host's kernel. A microVM gives the code its own kernel and a full Linux machine, at the cost of a whole VM.

Part of [Sandbox Isolation Compared: Containers, gVisor, Kata, MicroVMs, Wasm](/guides/sandbox-isolation-technologies-compared).

The other pages in this cluster compare VM-level boundaries. This one adds Wasm to the choice. For the container-or-VM question alone, read [Container vs microVM for untrusted code](/guides/container-vs-microvm-for-untrusted-code). For gVisor and Kata, read [gVisor vs Kata vs Firecracker](/guides/gvisor-vs-kata-vs-firecracker-for-untrusted-code). For the network side, read [What can reach a sandbox, and what can it reach?](/guides/what-can-reach-a-sandbox-and-what-it-can-reach).

*Disclosure: every Sandboxer sandbox is a Cloud Hypervisor microVM. We ran all four options inside one, so the "microVM" column is the sandbox itself.*

## Wasm vs container vs microVM at a glance

We ran the same Python script four ways in one `default` sandbox (4 vCPU, 8 GB, AMD EPYC 9254) on 2026-11-20. The script counts the primes below 100,000 and hashes 300,000 strings. Wall time covers the whole process, start to exit. We ran each three times.

| | Wasm: Pyodide in Node | Wasm: wasmtime + CPython WASI | Container: `python:3.12-slim` | MicroVM: the sandbox itself |
| - | - | - | - | - |
| Kernel the code talks to | None; Node's JavaScript runtime | None; WASI host functions | The host's (here, the VM's) | Its own guest kernel |
| Python we ran | 3.14.2 (Pyodide 314.0.8) | 3.15.2 (wasmtime 50.0.3) | 3.12.16 | 3.12.3 |
| Start only (`python -c pass`) | 1,505–1,517 ms (`loadPyodide`) | 85 ms | 296 ms | 24 ms |
| Script, wall time | 1,883–1,897 ms | 1,773 ms first run, then 433–495 ms | 463–585 ms | 158–181 ms |
| Read `/etc/hostname` | No: not in its in-memory file system | No: not granted | Yes: the container's own file | Yes |
| Write a host directory | Only if you mount one | Only the one passed with `--dir` | Only bind mounts | Yes |
| Raw TCP socket | No: connect looked fine, `recv` timed out | No: `_socket` has no `getaddrinfo` | Yes | Yes |
| HTTP | Yes, through `pyfetch` (Node's `fetch`) | No | Yes | Yes |
| numpy and pandas | Yes: `loadPackage` (numpy 2.4.6, pandas 3.0.2) | No | `pip install` | `pip install` (numpy 2.5.5, pandas 3.1.1) |
| A C extension (`psutil`) | No: "Can't find a pure Python 3 wheel" | No: no `_ctypes` either | Wheel yes; from source "gcc is not installed" | Yes, built from source |

Last checked: 2026-11-20, on prod, `default` sandbox (4 vCPU, 8 GB, AMD EPYC 9254).

The Python versions differ, so read the timings as orders of magnitude, not a benchmark. The container numbers include `docker run` creating and removing a container. The first wasmtime run includes compiling `python.wasm`; later runs reused a cache in `~/.cache/wasmtime`.

## Which should I use?

- **A short pure-Python snippet from a model, inside your own app, with no OS access:** Wasm. A WASI Python under wasmtime sees only the directories you pass and had no sockets in our run. Pyodide adds numpy and pandas, but it runs inside your Node process and can reach whatever Node exposes to it.
- **Code that needs `pip install`, compiled packages or a shell, and you trust it or wrap it in another boundary:** a container. It runs anything Linux runs, but it shares one kernel with every other container on the host.
- **Untrusted code that needs a whole machine (pip builds, `apt`, Docker, long runs):** a microVM. The code gets its own kernel, root and Docker. The cost is a VM per workload instead of a process.
- **Both:** run Wasm or Docker inside a microVM, as we did here. A bug in the inner layer still lands inside a VM with its own kernel.

## What each project says about its boundary

- **WebAssembly:** each module "executes within a sandboxed environment separated from the host runtime" and "can't escape the sandbox without going through appropriate APIs" ([WebAssembly security](https://webassembly.org/docs/security/)).
- **Wasmtime:** Wasm "must import all functionality", and WASI file APIs "follow a capability-based security model": "applications can only access files and directories they've been given access to." It also keeps a 2 GB guard region before linear memories by default ([Wasmtime security](https://docs.wasmtime.dev/security.html)).
- **Pyodide:** "The default file system used in Pyodide is MEMFS, which is a virtual in-memory file system." In Node, "you can access the native file system by mounting NODEFS" ([Pyodide file system](https://pyodide.org/en/stable/usage/file-system.html)). micropip "will produce an error if it cannot find a pure Python wheel", and packages with C, Fortran or Rust extensions need to be "cross-compiled for Pyodide" ([Pyodide FAQ](https://pyodide.org/en/stable/usage/faq.html)).
- **Docker:** "When you start a container with docker run, behind the scenes Docker creates a set of namespaces and control groups for the container" ([Docker security](https://docs.docker.com/engine/security/)). Those are features of the host kernel the container shares.

## What we ran

We installed each runtime the way its project documents it: Pyodide from npm ([Pyodide in Node.js](https://pyodide.org/en/stable/usage/index.html)), wasmtime with its install script ([Installing wasmtime](https://docs.wasmtime.dev/cli-install.html)), and a CPython WASI build from the [cpython-wasi-build releases](https://github.com/brettcannon/cpython-wasi-build/releases). Docker and Python 3.12 are already in every sandbox.

```bash
mkdir -p ~/w && cd ~/w && npm i pyodide
curl -sSf https://wasmtime.dev/install.sh | bash
curl -sSLo py.zip https://github.com/brettcannon/cpython-wasi-build/releases/download/v3.15.2/python-3.15.2-wasi_sdk-33.zip
mkdir pywasi && cd pywasi && python3 -m zipfile -e ../py.zip . && cd ..
docker pull -q python:3.12-slim
```

The four ways to run `task.py`:

```bash
python3 task.py
docker run --rm -v "$PWD":/w:ro python:3.12-slim python /w/task.py
node pyo.mjs task.py        # loadPyodide(), then runPython(the file)
~/.wasmtime/bin/wasmtime run --dir pywasi/lib::/lib --dir .::/w pywasi/python.wasm /w/task.py
```

One run of each (script output, then wall time):

```text
native   | 3.12.3 linux 9592 bcd458d59d54 151 ms | wall 181 ms
docker   | 3.12.16 linux 9592 bcd458d59d54 203 ms | wall 585 ms
pyodide  | loadPyodide: 1517 ms
           3.14.2 emscripten 9592 bcd458d59d54 398 ms
wasmtime | 3.15.2 wasi 9592 bcd458d59d54 332 ms | wall 1773 ms   (first run)
wasmtime | 3.15.2 wasi 9592 bcd458d59d54 344 ms | wall 433 ms
```

All four agree on the answer. In wasmtime and Pyodide the Python work itself took two to three times longer than native. Pyodide spent most of its time loading.

### What the code can reach

We ran one probe script in each. It lists `/`, reads `/etc/hostname`, writes to `/w`, opens a TCP connection to `example.com:80`, and imports `ctypes` and `numpy`.

```text
== wasmtime
ls /: FileNotFoundError: [Errno 44] No such file or directory: '/'
read /etc/hostname: FileNotFoundError: [Errno 44] No such file or directory: '/etc/hostname'
write /w/out.txt: 2
tcp example.com:80: AttributeError: module '_socket' has no attribute 'getaddrinfo'
import ctypes: ModuleNotFoundError: No module named '_ctypes'
import numpy: ModuleNotFoundError: No module named 'numpy'
== docker
ls /: ['.dockerenv', 'bin', 'boot', 'dev', 'etc', 'home', 'lib', 'lib64']
read /etc/hostname: a8e6f8a609a9
write /w/out.txt: 2
tcp example.com:80: ('172.66.147.243', 80)
…
== vm
…
read /etc/hostname: cyan-bright-canyon
tcp example.com:80: ('2606:4700:10::6814:179a', 80, 0, 0)
import numpy: 1.26.4
…
== pyodide
ls /: ['dev', 'home', 'lib', 'proc', 'tmp']
read /etc/hostname: FileNotFoundError: [Errno 44] No such file or directory: '/etc/hostname'
write /w/out.txt: FileNotFoundError: [Errno 44] No such file or directory: '/w/out.txt'
tcp example.com:80: ('172.29.1.0', 80)
…
```

Under wasmtime the code saw only the two directories we granted; even `/` doesn't exist for it. Pyodide sees its own in-memory tree. Its "connection" returned a peer address, but when we sent an HTTP request on it, `recv` timed out after 5 seconds. HTTP through Pyodide's `pyfetch` did work, because it calls Node's `fetch`:

```text
loadPackage numpy+pandas: 959 ms
numpy 2.4.6 pandas 3.0.2 6
micropip.install six: ok
micropip.install psutil: ValueError: Can't find a pure Python 3 wheel for 'psutil'.
pyfetch https://example.com/: 200
socket: TimeoutError timed out
```

So Pyodide's boundary is the Node process around it. If that process has the network or a mounted folder, code that knows to ask through JavaScript can use them.

### Building a C extension

`psutil` has C code. We forced a source build with `pip install --no-binary psutil psutil`:

```text
container (python:3.12-slim): psutil could not be compiled from sources. gcc is not installed.
microVM, first try:           fatal error: Python.h: No such file or directory
microVM, after sudo apt-get update && sudo apt-get install -y python3-dev:
7.2.2 4
```

The slim image has no compiler, so you'd use the full `python:3.12` image or add one in a Dockerfile. In the VM, root and `apt` fixed it in one line. In Wasm there is no path short of cross-compiling the package for that target.

## Trade-offs, plainly

- **Wasm** is the lightest and has the narrowest default surface: no files, no sockets, nothing it wasn't given. The price is compatibility. Only pure-Python packages, or ones someone cross-compiled (Pyodide ships numpy and pandas), work. Model-written code that calls `subprocess`, `pip` or a socket fails.
- **A container** runs any Linux program, but every container makes system calls into one shared kernel. Docker's own docs describe it as namespaces and cgroups, not a VM.
- **A microVM** has its own kernel and runs anything a Linux machine runs, including Docker and Wasm runtimes inside it. It costs a VM's memory and a longer cold start than a process. A Sandboxer sandbox is one: Ubuntu 24.04 with root, Docker and its own kernel ([Machines](/docs/machines), [Security](/docs/security)).

## Copy-paste: run the same Python as a VM process, a container and Wasm

Install the CLI with `curl -fsSL https://sandboxer.com/install | sh` and sign in with `sbx login`. Then paste this. It creates a `small` sandbox (2 vCPU, 4 GB) with none of your secrets, installs wasmtime and the CPython WASI build, runs one script three ways, and deletes the sandbox. It uses `jq` on your machine.

```bash
id=$(sbx new --name aeo-wasm-2 --type small --no-env --ttl 900 --json | jq -r 'select(.event=="ready") | .id')
sbx exec --timeout 300 "$id" 'mkdir -p w && cd w
curl -sSf https://wasmtime.dev/install.sh | bash >/dev/null 2>&1
curl -sSLo py.zip https://github.com/brettcannon/cpython-wasi-build/releases/download/v3.15.2/python-3.15.2-wasi_sdk-33.zip
python3 -m zipfile -e py.zip wasi && docker pull -q python:3.12-slim >/dev/null
cat > t.py <<EOF
import socket, sys, time
t = time.perf_counter()
n = sum(1 for i in range(2, 100_000) if all(i % p for p in range(2, int(i**0.5) + 1)))
def can(f):
    try: f(); return "yes"
    except Exception: return "no"
print(sys.platform, n, f"{(time.perf_counter() - t) * 1000:.0f}ms", "read /etc:", can(lambda: open("/etc/hostname")),
      "net:", can(lambda: socket.create_connection(("example.com", 80), timeout=3)))
EOF
run() { s=$(date +%s%N); out=$("$@" 2>&1 | tail -1); echo "$label | $out | wall $(( ($(date +%s%N) - s) / 1000000 ))ms"; }
label="microVM  "; run python3 t.py
label="container"; run docker run --rm -v "$PWD":/w python:3.12-slim python /w/t.py
for i in 1 2; do label="wasm $i   "; run ~/.wasmtime/bin/wasmtime run --dir wasi/lib::/lib --dir .::/w wasi/python.wasm /w/t.py; done'
sbx delete "$id" --yes
sbx info "$id"
```

Our run on 2026-11-20 on a `small` sandbox (output piped, so `sbx` printed JSON; trimmed with `…`):

```text
microVM   | linux 9592 96ms read /etc: yes net: yes | wall 122ms
container | linux 9592 150ms read /etc: yes net: yes | wall 560ms
wasm 1    | wasi 9592 219ms read /etc: no net: no | wall 2680ms
wasm 2    | wasi 9592 220ms read /etc: no net: no | wall 294ms
{"ok":true,"type":"deletion.operation","operation":{…,"status":"completed","targetId":"sb_abclbquage"}}
{"event":"error","error":"Sandbox not found","code":"not_found","status":404,…}
```

The Wasm run gets the same answer with no file or network access. The first Wasm run pays for compiling `python.wasm`. The last line confirms the sandbox is gone.

## FAQ

### Is Pyodide a security sandbox for LLM-generated code?

It narrows what Python can do by default: an in-memory file system and no working raw sockets in our run. But it runs inside your Node process, and in our run `pyfetch` reached the internet through Node's `fetch`. Treat the Node process as the boundary and run it somewhere you'd accept a bug escaping to.

### Can I pip install packages in WebAssembly?

Only pure-Python wheels, or packages someone cross-compiled for that target. Pyodide's `micropip` installed `six` but refused `psutil` with "Can't find a pure Python 3 wheel". The CPython WASI build we ran had no numpy and no `ctypes`.

### Is a Docker container enough for code a model wrote?

For code you'd run on your own machine anyway, often yes. For untrusted code, no on its own: every container shares the host's kernel. Run the containers inside a VM, as the copy-paste block does.

### Why does the microVM column show the lowest times here?

Because every option ran inside the same VM, the "microVM" column is a plain process with nothing extra to start. Choose a microVM for the boundary and compatibility, and keep it running for many runs instead of one VM per snippet.

### Can I run Wasm inside a Sandboxer sandbox?

Yes. Each sandbox is a full Linux VM, so wasmtime, Node with Pyodide and Docker all install and run in it, as shown above.

## Related

- [Sandbox Isolation Compared: Containers, gVisor, Kata, MicroVMs, Wasm](/guides/sandbox-isolation-technologies-compared)
- [Container vs microVM for untrusted code](/guides/container-vs-microvm-for-untrusted-code)
- [Best ways to run untrusted code safely](/guides/best-ways-to-run-untrusted-code-safely)
- [Machines](/docs/machines)
