-
Notifications
You must be signed in to change notification settings - Fork 0
WASM Search
ABCrimson edited this page Mar 11, 2026
·
2 revisions
Optional Rust-compiled trigram index for sub-millisecond fuzzy search on 100K+ items.
pnpm add modern-cmdk-search-wasm
import { createWasmSearchEngine } from 'modern-cmdk-search-wasm'; // await using for automatic cleanup await using engine = await createWasmSearchEngine(); engine.index(items); const results = engine.search('query', items).toArray();
For non-blocking UI with large datasets:
import { createWorkerWasmSearchEngine } from 'modern-cmdk-search-wasm'; await using engine = await createWorkerWasmSearchEngine(); engine.index(items); const results = engine.search('query', items).toArray();
Worker mode with SharedArrayBuffer requires these HTTP headers:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Without these headers, the worker falls back to postMessage with structured cloning.
import { createCommandMachine } from 'modern-cmdk'; import { createWasmSearchEngine } from 'modern-cmdk-search-wasm'; const wasmEngine = await createWasmSearchEngine(); using machine = createCommandMachine({ items, search: wasmEngine, // Pluggable search interface });
The WASM module is built from a Rust crate at packages/command-search-wasm/crate/:
-
trigram.rs— Trigram index construction and lookup -
scorer.rs— Fuzzy scoring algorithm -
lib.rs— wasm-bindgen bindings
| Operation | JS Scorer | WASM (Main Thread) | WASM (Worker) |
|---|---|---|---|
| Index 10K | ~5ms | ~2ms | ~2ms (off-thread) |
| Search 10K | ~5ms | ~0.8ms | ~0.8ms (off-thread) |
| Search 100K | ~50ms | ~3ms | ~3ms (off-thread) |