---
id: wiki-2026-0508-웹-워커-이벤트-포워딩-통신-지연-최소화-방법
title: 웹 워커 이벤트 포워딩 통신 지연 최소화 방법
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Web Worker Event Forwarding, Worker Message Latency, postMessage Optimization]
duplicate_of: none
source_trust_level: A
confidence_score: 0.88
verification_status: applied
tags: [frontend, web-worker, performance, postmessage, transferable, comlink]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: TypeScript
  framework: Web Workers, Comlink, SharedArrayBuffer
---

# 웹 워커 이벤트 포워딩 통신 지연 최소화 방법

## 매 한 줄
> **"매 main thread ↔ Worker 통신 의 latency 의 4가지 의 source: serialization, structured clone, queue, dispatch — 매 Transferable / SharedArrayBuffer / batching / Comlink RPC 의 mitigation"**. 매 postMessage 의 default behavior 의 매 bytes 의 deep clone — 매 large payload 의 매 ms 의 단위. 매 2026 의 modern approach 의 OffscreenCanvas, transferable streams, Atomics.

## 매 핵심

### 매 Latency Source
1. **Structured clone** — 매 object 의 deep copy, 매 size 의 비례.
2. **Serialization** — 매 V8 의 internal format 의 conversion.
3. **Event queue** — 매 receiver 의 task queue 의 enqueue.
4. **Dispatch overhead** — 매 microtask boundary, 매 ~0.1-0.5ms.

### 매 Mitigation 전략
- **Transferable Objects** — `ArrayBuffer`, `MessagePort`, `OffscreenCanvas`, `ReadableStream` 의 zero-copy 의 ownership transfer.
- **SharedArrayBuffer + Atomics** — 매 shared memory, 매 lock-free 통신.
- **Batching** — 매 multiple event 의 single message 의 합침.
- **MessageChannel** — 매 dedicated channel, 매 main thread 의 bypass.
- **Comlink** — 매 RPC abstraction, 매 ergonomics.

### 매 Transferable Objects (2026)
- `ArrayBuffer`, `MessagePort`, `ImageBitmap`, `OffscreenCanvas`, `RTCDataChannel`.
- `ReadableStream`, `WritableStream`, `TransformStream` (Streams API).
- `VideoFrame`, `AudioData` (WebCodecs).
- 매 transfer 후 매 sender 의 access 의 X (neutered).

## 💻 패턴

### Pattern 1: Transferable ArrayBuffer (zero-copy)
```ts
// main.ts
const buffer = new ArrayBuffer(10_000_000); // 10MB
const view = new Float32Array(buffer);
view.fill(1.0);

worker.postMessage({ buffer }, [buffer]); // 매 두번째 인자 의 transfer list
console.log(buffer.byteLength); // 0 — 매 neutered

// worker.ts
self.onmessage = (e) => {
  const buffer: ArrayBuffer = e.data.buffer;
  // 매 zero-copy 의 ownership 의 받음
  const view = new Float32Array(buffer);
  // ... process
};
```

### Pattern 2: Comlink RPC (ergonomic)
```ts
// worker.ts
import * as Comlink from 'comlink';

const api = {
  async heavyCompute(data: Float32Array): Promise<Float32Array> {
    // ... compute
    return result;
  },
  async streamResults(onChunk: (c: number[]) => void) {
    for (let i = 0; i < 100; i++) {
      onChunk([Math.random()]);
      await new Promise(r => setTimeout(r, 10));
    }
  },
};
Comlink.expose(api);

// main.ts
import * as Comlink from 'comlink';
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
const api = Comlink.wrap<typeof api>(worker);
const result = await api.heavyCompute(input);
```

### Pattern 3: SharedArrayBuffer + Atomics (lock-free)
```ts
// 매 COOP/COEP header 의 필요 — Cross-Origin-Opener-Policy: same-origin
const sab = new SharedArrayBuffer(1024);
const view = new Int32Array(sab);
worker.postMessage({ sab });

// worker 의 spinlock 의 wait
Atomics.wait(view, 0, 0); // 매 view[0]==0 의 동안 sleep
const data = view[1];

// main 의 signal
view[1] = 42;
view[0] = 1;
Atomics.notify(view, 0, 1); // 매 worker 의 wake
```

### Pattern 4: Batching (event coalescing)
```ts
// main.ts
const queue: Event[] = [];
let scheduled = false;

function forward(event: Event) {
  queue.push(event);
  if (!scheduled) {
    scheduled = true;
    queueMicrotask(() => {
      worker.postMessage({ events: queue.splice(0) });
      scheduled = false;
    });
  }
}

window.addEventListener('mousemove', forward); // 매 60fps 의 single batch
```

### Pattern 5: OffscreenCanvas (rendering off main thread)
```ts
// main.ts
const canvas = document.querySelector('canvas')!;
const offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);

// worker.ts
self.onmessage = (e) => {
  const canvas = e.data.canvas as OffscreenCanvas;
  const ctx = canvas.getContext('2d')!;
  // 매 main thread 의 block X 의 render
  function frame() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // ... draw
    requestAnimationFrame(frame);
  }
  frame();
};
```

### Pattern 6: MessageChannel (direct port)
```ts
const channel = new MessageChannel();
worker1.postMessage({ port: channel.port1 }, [channel.port1]);
worker2.postMessage({ port: channel.port2 }, [channel.port2]);
// 매 worker 끼리 매 main thread 의 bypass 의 직접 통신
```

### Pattern 7: Transferable Stream (2026)
```ts
// main.ts
const stream = new ReadableStream({
  start(ctrl) {
    fetch('/large').then(async r => {
      const reader = r.body!.getReader();
      while (true) {
        const { value, done } = await reader.read();
        if (done) break;
        ctrl.enqueue(value);
      }
      ctrl.close();
    });
  }
});
worker.postMessage({ stream }, [stream]); // 매 zero-copy

// worker.ts
self.onmessage = async (e) => {
  for await (const chunk of e.data.stream) {
    process(chunk);
  }
};
```

## 매 결정 기준
| 상황 | 기법 |
|---|---|
| Large binary payload | Transferable ArrayBuffer |
| RPC ergonomics | Comlink |
| Lock-free shared state | SharedArrayBuffer + Atomics |
| High-frequency events | Batching with `queueMicrotask` |
| Canvas rendering | OffscreenCanvas |
| Worker-to-worker | MessageChannel |
| Streaming data | Transferable streams |

**기본값**: Transferable ArrayBuffer + Comlink RPC + 매 60fps event 의 batching.

## 🔗 Graph
- 부모: [[Web Worker (웹 워커)|Web Workers]] · [[웹 프론트엔드 성능 최적화|Frontend Performance Optimization (FE 성능 최적화)]]
- 변형: [[Service Worker]] · [[SharedWorker]] · [[Worklet]]
- Adjacent: [[Event Loop]] · [[Structured Clone]] · [[Comlink]]

## 🤖 LLM 활용
**언제**: postMessage 의 large payload 의 transferable 의 변환 권고, COOP/COEP header 의 troubleshoot, batching 의 적절한 frequency 의 제안.
**언제 X**: 매 native thread (C++ worker) 의 통신 — 매 다른 domain.

## ❌ 안티패턴
- **Large object 의 매 postMessage**: 매 deep clone, 매 GC pressure, 매 main thread 의 stall.
- **Per-event 매 postMessage** (mousemove): 매 queue 의 폭주, 매 jank.
- **Atomics 의 SharedArrayBuffer 없음**: 매 cross-origin isolation 의 필요.
- **Worker 의 dynamic import 의 누락**: 매 type: 'module' 의 누락.
- **Transferable 의 후 의 access**: 매 neutered, 매 byteLength=0.

## 🧪 검증 / 중복
- Verified (HTML spec: Workers, MDN Transferable Objects, Comlink GitHub 2026).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Transferables, Comlink RPC, SAB+Atomics, batching patterns |
