💻 Programming

WebAssembly

WebAssembly (WASM)

A low-level binary format that runs in the browser. Near-native performance.

📖 Detailed Explanation

WebAssembly (WASM) is a low-level binary instruction format that runs in web browsers. It is an innovative technology that makes it possible to run code written in high-performance languages such as C, C++, and Rust on the web at near-native speed. It was created to overcome the performance limitations of JavaScript and was designed to work alongside existing web technologies.

WebAssembly was adopted as an official web standard by the W3C in 2017. It was developed in collaboration by major browser vendors including Mozilla, Google, Microsoft, and Apple, and is currently supported in all major browsers. In 2019, it was promoted to an official W3C Recommendation, establishing itself as the fourth language of the web (HTML, CSS, JS, WASM).

The reason WASM achieves near-native performance lies in the efficiency of its binary format. Unlike text-based JavaScript, WASM is precompiled binary code, so parsing and compilation time are greatly reduced. In addition, thanks to its static type system, linear memory model, and stack-based virtual machine architecture, the browser's JIT compiler can generate highly optimized machine code.

In practice, WebAssembly is used for compute-intensive tasks such as game engines (Unity, Unreal Engine), image/video processing (Photoshop Web, Figma), in-browser AI inference (TensorFlow.js WASM backend), cryptographic operations, and CAD applications. In addition, its use is expanding to serverless environments and edge computing through WASI (WebAssembly System Interface).

💻 Code Example

// Rust에서 WASM 모듈 작성하기
// Cargo.toml: wasm-bindgen, wasm-pack 사용

use wasm_bindgen::prelude::*;

// JavaScript에서 호출할 수 있는 함수 내보내기
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

// 문자열 처리 예제
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("안녕하세요, {}님! WASM에서 인사드립니다.", name)
}

// 이미지 처리 예제: 픽셀 밝기 조절
#[wasm_bindgen]
pub fn adjust_brightness(pixels: &mut [u8], factor: f32) {
    for pixel in pixels.iter_mut() {
        let adjusted = (*pixel as f32 * factor).min(255.0) as u8;
        *pixel = adjusted;
    }
}

// 컴파일: wasm-pack build --target web

🗣️ Say It Like This in Practice

💬 In a Meeting (Performance Optimization Strategy)
"Image filter processing only reaches 30fps with JavaScript, but if we write the core logic in Rust and compile it to WASM, we can raise it to 60fps. Pixel-level operations in particular are 5 to 10 times faster with WASM. Integrating with JS is also simple with wasm-bindgen."
💬 In an Interview (Pros and Cons of WASM)
"The advantages of WebAssembly are near-native performance, support for a variety of languages, and sandbox security. The disadvantages are that it cannot access the DOM directly, so JavaScript glue code is required, debugging is difficult, and the WASM file must be downloaded on initial load. So rather than building everything in WASM, it is better to optimize only the compute-intensive parts with WASM."
💬 In a Tech Blog
"The reason Figma adopted WASM is clear. They wrote core engines such as vector rendering and layout calculation in C++ and compiled them to WASM, achieving desktop-app-level performance even in the browser. As a result, it can be used directly in the browser without installation."
💬 In a Code Review - WASM Memory Management
"This Rust WASM function returns a Vec, but if the JavaScript side doesn't free the memory, it will leak. Even when using wasm-bindgen, large buffers need to be freed manually by calling free(). Also, in the adjust_brightness function, instead of copying the pixel array, accessing it directly via SharedArrayBuffer will give better performance. Lastly, the WASM file is 1.2MB — did you apply wasm-opt -O3? And with brotli compression, you can reduce it to under 300KB."

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

⚠️ Common Mistakes & Cautions

❌
No Direct DOM Access

WebAssembly cannot access the DOM API directly. If you need to manipulate the UI, you must go through JavaScript glue code. Use wasm-bindgen or Emscripten's bindings.

❌
Difficult debugging

WASM binaries are tricky to debug. Generate source maps or include DWARF debug information, and use the WASM debugger in Chrome DevTools. Use debug builds during development.

❌
Bundle Size Management

WASM files can be several MB in size. Optimize with wasm-opt, include only the necessary features, and apply lazy loading. gzip/brotli compression is also essential.

✅
Choosing the Right Use Cases

For simple DOM manipulation or business logic, JavaScript is more suitable. Use WASM for CPU-intensive tasks such as image/video processing, game engines, cryptography, and physics simulation.

🔗 Related Terms

📚 Learn More