When to Use HNSW
| Documents | Recommendation |
|---|---|
| < 1,000 | Brute-force (default) |
| 1,000 - 10,000 | Either works |
| > 10,000 | Use HNSW |
| > 100,000 | HNSW required |
Basic Configuration
src/hnsw-config.ts
Performance Comparison
Search Time
| Documents | Brute-force | HNSW (ef=50) |
|---|---|---|
| 10,000 | ~50ms | ~1ms |
| 50,000 | ~250ms | ~1ms |
| 100,000 | ~500ms | ~2ms |
Build Time
| Documents | M=16, ef=200 |
|---|---|
| 10,000 | ~5 seconds |
| 50,000 | ~30 seconds |
| 100,000 | ~2 minutes |
Memory Usage
HNSW adds approximately 50-100 bytes per document for graph connections on top of the embedding storage.How HNSW Works
HNSW creates a multi-layer graph structure:- Hierarchical layers: Upper layers have fewer nodes for fast navigation
- Navigable small world: Nodes connect to similar nodes nearby
- Approximate search: Trades exact results for speed (95%+ recall)
Incremental Updates
HNSW supports incremental updates without full rebuilds:src/hnsw-incremental.ts
Persistence with HNSW
The HNSW index structure is persisted along with embeddings:src/hnsw-persistence.ts
Related
HNSW Configuration
Parameter reference
HNSW Tuning
Optimize for your use case
Search Performance
General performance tips