This guide provides instructions for building the Ruvector WASM bindings. The WASM module enables high-performance vector database operations directly in web browsers and Node.js environments.
✅ Completed Components:
-
Core WASM Bindings (
/crates/ruvector-wasm/src/lib.rs)- Full VectorDB API (insert, search, delete, batch operations)
- Proper error handling with WasmResult types
- Console panic hook for debugging
- JavaScript-compatible types (JsVectorEntry, JsSearchResult)
-
SIMD Support
- Dual build configuration (with/without SIMD)
- Feature flags in Cargo.toml
- Runtime SIMD detection via
detectSIMD()function
-
Web Workers Integration (
/crates/ruvector-wasm/src/worker.js)- Message passing for async operations
- Support for insert, search, delete, batch operations
- Zero-copy transfers preparation
-
Worker Pool Management (
/crates/ruvector-wasm/src/worker-pool.js)- Automatic pool sizing (4-8 workers based on CPU cores)
- Round-robin task distribution
- Promise-based API
- Error handling and timeouts
-
IndexedDB Persistence (
/crates/ruvector-wasm/src/indexeddb.js)- Save/load vectors to IndexedDB
- Batch operations for performance
- Progressive loading with callbacks
- LRU cache implementation (1000 hot vectors)
-
Examples
- Vanilla JavaScript example (
/examples/wasm-vanilla/index.html) - React + Web Workers example (
/examples/wasm-react/)
- Vanilla JavaScript example (
-
Tests
- Comprehensive WASM tests (
/crates/ruvector-wasm/tests/wasm.rs) - Browser-based testing with wasm-bindgen-test
- Comprehensive WASM tests (
-
Build Configuration
- Optimized for size (target: <500KB gzipped)
- Multiple build targets (web, nodejs, bundler)
- Size verification scripts
# Install Rust with wasm32 target
rustup target add wasm32-unknown-unknown
# Install wasm-pack
cargo install wasm-pack
# Optional: Install wasm-opt for further optimization
npm install -g wasm-optcd crates/ruvector-wasm
wasm-pack build --target web --out-dir pkg --releasecd crates/ruvector-wasm
wasm-pack build --target web --out-dir pkg-simd --release -- --features simdcd crates/ruvector-wasm
npm run build:allThis will build for:
- Web (
pkg/) - Web with SIMD (
pkg-simd/) - Node.js (
pkg-node/) - Bundler (
pkg-bundler/)
Problem: Some dependencies (notably rand via uuid) pull in getrandom 0.3.4, which requires the wasm_js feature flag that must be set via RUSTFLAGS configuration flags, not just Cargo features.
Solution Options:
-
Use .cargo/config.toml (Already configured):
[target.wasm32-unknown-unknown] rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
-
Disable uuid feature (Implemented):
# In ruvector-core/Cargo.toml [features] default = ["simd", "uuid-support"] uuid-support = ["uuid"] # In ruvector-wasm/Cargo.toml [dependencies] ruvector-core = { path = "../ruvector-core", default-features = false }
-
Alternative: Use timestamp-based IDs (Fallback): For WASM builds, use
Date.now()+ random suffixes instead of UUIDs
Solution:
-
Enable LTO and size optimization (already configured):
[profile.release] opt-level = "z" lto = true codegen-units = 1 panic = "abort"
-
Run wasm-opt:
npm run optimize
-
Verify size:
npm run size
<!DOCTYPE html>
<html>
<head>
<title>Ruvector WASM</title>
</head>
<body>
<script type="module">
import init, { VectorDB } from './pkg/ruvector_wasm.js';
await init();
const db = new VectorDB(384, 'cosine', true);
// Insert vector
const vector = new Float32Array(384).map(() => Math.random());
const id = db.insert(vector, 'vec_1', { label: 'test' });
// Search
const query = new Float32Array(384).map(() => Math.random());
const results = db.search(query, 10);
console.log('Results:', results);
</script>
</body>
</html>import { WorkerPool } from '@ruvector/wasm/worker-pool';
const pool = new WorkerPool(
'/worker.js',
'/pkg/ruvector_wasm.js',
{
poolSize: 4,
dimensions: 384,
metric: 'cosine'
}
);
await pool.init();
// Parallel insert
const entries = Array(1000).fill(0).map((_, i) => ({
vector: Array(384).fill(0).map(() => Math.random()),
id: `vec_${i}`,
metadata: { index: i }
}));
const ids = await pool.insertBatch(entries);
// Parallel search
const results = await pool.search(query, 10);
// Cleanup
pool.terminate();import { IndexedDBPersistence } from '@ruvector/wasm/indexeddb';
const persistence = new IndexedDBPersistence('my_database');
await persistence.open();
// Save vectors
await persistence.saveBatch(vectors);
// Load with progress
await persistence.loadAll((progress) => {
console.log(`Loaded ${progress.loaded} vectors`);
if (progress.vectors.length > 0) {
db.insertBatch(progress.vectors);
}
});
// Get stats
const stats = await persistence.getStats();
console.log(`Cache hit rate: ${(stats.cacheHitRate * 100).toFixed(2)}%`);cd crates/ruvector-wasm
wasm-pack test --headless --chrome
wasm-pack test --headless --firefoxwasm-pack test --node- Enable SIMD: Use the SIMD build for 2-4x speedup on supported browsers
- Use Batch Operations:
insertBatchis 5-10x faster than multipleinsertcalls - Use Web Workers: Distribute operations across workers for parallel processing
- Enable LRU Cache: Keep hot vectors in IndexedDB cache
- Optimize Vector Size: Smaller dimensions = faster operations
- Choose Appropriate Metric: Dot product is fastest, Euclidean is slowest
| Browser | Version | SIMD Support | Web Workers | IndexedDB |
|---|---|---|---|---|
| Chrome | 91+ | ✅ | ✅ | ✅ |
| Firefox | 89+ | ✅ | ✅ | ✅ |
| Safari | 16.4+ | Partial | ✅ | ✅ |
| Edge | 91+ | ✅ | ✅ | ✅ |
Expected sizes after optimization:
- Base build: ~450KB gzipped
- SIMD build: ~480KB gzipped
- With wasm-opt -Oz: ~380KB gzipped
Ensure your server sends proper CORS headers:
{
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp'
}Increase WASM memory limit if needed:
// In worker.js or main thread
WebAssembly.instantiate(module, {
env: {
memory: new WebAssembly.Memory({ initial: 256, maximum: 512 })
}
});Check available storage:
if ('storage' in navigator && 'estimate' in navigator.storage) {
const estimate = await navigator.storage.estimate();
console.log(`Using ${estimate.usage} of ${estimate.quota} bytes`);
}- Complete Build Debugging: Resolve getrandom compatibility issues
- Add More Examples: Vue.js, Svelte, Angular examples
- Benchmarking Suite: Compare performance across browsers
- CDN Distribution: Publish to npm and CDNs
- Documentation: Interactive playground and tutorials
See main repository for contribution guidelines.
MIT