🌐 Web Development

WASM

WebAssembly

A binary format that runs in the browser. Native-level performance.

📖 Detailed Explanation

WebAssembly (WASM) is a binary instruction format that runs in the browser at speeds close to native. Code written in languages such as C, C++, Rust, and Go can be compiled into a .wasm binary and run on the web. It is used for compute-intensive tasks that go beyond the performance limits of JavaScript.

WASM began to be supported in major browsers in 2017. It runs on top of the JavaScript virtual machine, but because it is precompiled bytecode, there is almost no parsing time and it is converted into optimized native code. For security, it runs in the same sandbox as JavaScript.

The main use cases of WASM are game engines (Unity, Unreal), video/image processing (FFmpeg, Photoshop Web), cryptographic computation, scientific simulation, and so on. The web versions of Figma, Google Earth, and AutoCAD use WASM. It is also useful for porting existing C/C++ codebases to the web.

WASM does not replace JavaScript. Because direct DOM access is not possible, it must work together with JavaScript. In addition, through WASI (WebAssembly System Interface) it can now run outside the browser, so it is also used in serverless functions and plugin systems.

💻 Code Example

// JavaScript에서 WebAssembly 로드 및 실행

// 1. .wasm 파일 로드 (기본 방법)
const response = await fetch('module.wasm');
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes);

// 2. WASM 함수 호출
const result = instance.exports.add(10, 20);
console.log(result); // 30

// 3. 스트리밍 컴파일 (더 효율적)
const { instance: streamInstance } = await WebAssembly.instantiateStreaming(
  fetch('module.wasm'),
  { env: { log: console.log } } // import 객체
);

// Rust로 WASM 만들기 (wasm-bindgen 사용)
// Cargo.toml
// [lib]
// crate-type = ["cdylib"]
// [dependencies]
// wasm-bindgen = "0.2"

// src/lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2)
    }
}

// JavaScript에서 사용
import init, { fibonacci } from './pkg/my_wasm.js';
await init();
console.log(fibonacci(40)); // JS보다 훨씬 빠름

🗣️ Say It Like This in Practice

💬 In a Technical Review
"If we process images on the client, we can reduce the server load. JavaScript Canvas is slow, so if we handle it with a WASM module written in Rust, we get performance close to a native app. Integrating with JS is also easy with wasm-bindgen."
💬 In an Interview
"WebAssembly is low-level bytecode that JavaScript engines can compile quickly. For compute-intensive tasks it can be dozens of times faster than JavaScript, but DOM access and Web API calls must go through JavaScript. It is important to combine them appropriately depending on the use."
💬 In a Performance Discussion
"Thanks to WASM, Figma behaves like a native app in the browser. If we also port our complex graph computation logic to Rust and compile it to WASM, we think we can significantly reduce loading time."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Trying to replace everything with WASM

WASM is suited to compute-intensive tasks. For general UI logic or DOM manipulation, JavaScript is more efficient. Also consider the JS-WASM call overhead.

❌
Ignoring .wasm file size

If the WASM binary is large, initial loading becomes slow. Optimize with code splitting, lazy loading, and compression (gzip/brotli).

✅
Proper Usage

Apply WASM to CPU-intensive tasks such as image/video processing, cryptography, simulation, and game engines. The combination of Rust + wasm-bindgen offers a good development experience.

🔗 Related Terms

📚 Learn More