This demo shows the difference between running WebAssembly in the main thread (blocking) vs in a Web Worker (non-blocking).
Windows (PowerShell):
.\build.ps1Linux/Mac (Bash):
chmod +x build.sh
./build.shThis will create:
web/hello.js- WASM module for main threadweb/hello-worker.js- WASM module for Web Worker
Windows (PowerShell):
.\server.ps1Linux/Mac (Bash):
chmod +x server.sh
./server.shNavigate to: http://localhost:8000/demo.html
The demo page has two panels side-by-side:
- Runs WASM directly in the main thread
- Blocks the UI during heavy computation
- Try clicking the "Increment Counter" button while heavy computation is running - it won't respond!
- Runs WASM in a background worker thread
- Keeps UI responsive during computation
- Try clicking the "Increment Counter" button while heavy computation is running - it responds immediately!
add(a, b)- Simple addition (instant, for quick testing)heavy_computation(iterations)- CPU-intensive computation that demonstrates blocking behavior
src/hello.c- C source code with WASM functionsweb/demo.html- Comparison demo pageweb/index.html- Original simple demoweb/worker.js- Web Worker implementationbuild.sh/build.ps1- Build scriptsserver.sh/server.ps1- HTTP server scripts
- Emscripten - For compiling C to WASM
- Python 3 - For the HTTP server (or use any HTTP server)
- Modern browser - Chrome, Firefox, Edge, or Safari with WebAssembly support
If Python isn't available, you can use:
# Node.js
npx http-server web -p 8000
# PHP
php -S localhost:8000 -t web
# Or any other HTTP server