webassembly-wave
webassembly-wave copied to clipboard
JavaScript vs WebAssembly performance comparison - wave equation demo
webassembly-wave
Simple benchmark comparing performance of JavaScript vs. WebAssembly modules generated by different compilers.
Description
This is a simulation of the wave equation ∂2u/∂t2 = c2(∂2u/∂x2+∂2u/∂y2) across a 2D manifold with a boundary condition of u=0 along the unit circle. Use the mouse to create waves.
It contains 5 implementations of the same code:
- Standard JavaScript
- C code compiled by Clang/Binaryen (WebAssembly Studio)
- C code compiled by Enscripten
- JS-like code compiled by Walt to WebAssembly
- TypeScript-like code compiled by AssemblyScript to WebAssembly
This particular algorithm processes large arrays but does no floating point calculations. Memory is visible to bot JS and WebAssembly and almost all CPU time is spent in the algorithm itself. (The canvas API introduces a minor overhead of about 10%.)
Compiling program
There are two options for generating the .wasm file:
- C: In directory
emscripten
compileemscripten.c
with Emscripten:emcc emscripten.c -Os -s WASM=1 -s SIDE_MODULE=1 -o emscripten.wasm
- Walt: In directory
walt
runnpm install
andnpm run build
to compilewaves.walt
to awaves.wasm
file. - AssemblyScript: In directory
as
runnpm install
andnpm run asbuild
to compileindex.ts
to wasm files.
Other stuff
-
test.html loads the JS and .wasm modules, tests them on a 5x5 grid, and and displays the results.
-
Directory
online_versions
has copies of older versions on online IDEs.-
webassembly_studio
has a version comparing JS and C, available at WebAssembly Studio -
codepen
has an older version with .wasm bundled inline as a base64 string. (Codepen isn't as useful for benchmarking since it mangles JS but not WebAssembly.)
-
Author
Jason Tiscione
License
This project is licensed under the MIT License.