WebAssembly: When Not to Use It
WebAssembly is the wrong tool for small or page-shaped jobs. The cost of loading a module can beat the speed it saves.
A menu toggle is a JavaScript job
One click opens a menu with plain JavaScript.
const menu = document.querySelector("#menu");
menu.classList.toggle("open");A tiny sum is a JavaScript job
A small sum needs no module at all.
console.log(2 + 3);Example
const total = 2 + 3;
console.log(total);
console.log("No module needed.");Key points
- Use plain JavaScript for buttons, menus, and form checks.
- Use plain JavaScript for small apps with little computing.
- WebAssembly cannot touch the page, so JavaScript must pass every change across.
- Short trips between JavaScript and WebAssembly add delay each time.
- A large module can slow the first load more than it speeds the work.
In this example, plain JavaScript adds 2 and 3 and says no module was needed. A tiny job like this is faster without WebAssembly. WebAssembly is the wrong tool when the job is small.
Note
Measure the real gain before you add WebAssembly to a page.
Try it Yourself
Edit the code, then press Run.
Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
A page only toggles a menu and checks a form. What fits best?