Skip to main content
Learn how to tune HNSW parameters for different scenarios.

Tuning Guidelines

For Real-Time Search Applications

Prioritize low latency over maximum recall:
src/hnsw-realtime.ts
const realtime = new VectoriaDB({
  useHNSW: true,
  hnsw: {
    M: 16,
    efConstruction: 200,
    efSearch: 40, // Low for speed
  },
});

For High-Precision Applications

Prioritize accuracy over speed:
src/hnsw-precision.ts
const precision = new VectoriaDB({
  useHNSW: true,
  hnsw: {
    M: 24,
    efConstruction: 400,
    efSearch: 200, // High for accuracy
  },
});

For Memory-Constrained Environments

Minimize memory footprint:
src/hnsw-low-memory.ts
const lowMemory = new VectoriaDB({
  useHNSW: true,
  hnsw: {
    M: 8,  // Lower M uses less memory
    efConstruction: 100,
    efSearch: 30,
  },
});

Dynamic efSearch

Adjust efSearch per-query based on requirements:
src/dynamic-ef-search.ts
async function search(query: string, precision: 'low' | 'medium' | 'high') {
  const efSearch = {
    low: 20,
    medium: 50,
    high: 200,
  }[precision];

  return db.search(query, {
    topK: 10,
    efSearch,
  });
}

// Fast search for autocomplete
const suggestions = await search(input, 'low');

// High-precision search for final results
const results = await search(query, 'high');

Recall vs Speed Trade-offs

            Recall
              ^
        99% --|------------------o Quality
              |                /
        97% --|------------o Balanced
              |          /
        95% --|------o Speed
              |    /
        93% --|--o Fast
              |
              +----+----+----+----+---> Speed
                  1ms  2ms  5ms  10ms

Benchmarking Your Configuration

src/benchmark.ts
async function benchmarkHNSW(config: HNSWConfig) {
  const db = new VectoriaDB({ useHNSW: true, hnsw: config });
  await db.initialize();

  // Build benchmark
  const buildStart = Date.now();
  await db.addMany(documents);
  const buildTime = Date.now() - buildStart;

  // Search benchmark
  const searchTimes: number[] = [];
  for (const query of testQueries) {
    const start = Date.now();
    await db.search(query);
    searchTimes.push(Date.now() - start);
  }

  const avgSearchTime = searchTimes.reduce((a, b) => a + b) / searchTimes.length;

  return { buildTime, avgSearchTime };
}

Common Issues

Poor Recall

If results aren’t relevant enough:
  1. Increase efSearch for search-time improvement
  2. Increase M and efConstruction, then rebuild index
If search is too slow:
  1. Decrease efSearch
  2. Consider lower M if memory allows

Slow Indexing

If bulk indexing is too slow:
  1. Decrease efConstruction
  2. Consider building without HNSW, then enabling it
Use CaseMefConstructionefSearch
Autocomplete1210020
General search1620050
High-precision24400100
Maximum recall32400200

HNSW Overview

Introduction to HNSW

HNSW Configuration

Parameter reference

Search Performance

General performance tips