Skip to main content
Learn about HNSW parameters and how they affect performance.

Parameter Reference

OptionDefaultDescription
M16Connections per node in layer > 0 (higher = better recall)
M032Connections for layer 0 (typically M x 2)
efConstruction200Candidate list size during construction
efSearch50Candidate list size during search

M (Max Connections)

Controls the number of connections each node has in the graph.
src/hnsw-m-param.ts
// Low M (8-12) - faster build, less memory, lower recall
const fast = new VectoriaDB({
  useHNSW: true,
  hnsw: { M: 8 },
});

// High M (24-48) - slower build, more memory, higher recall
const accurate = new VectoriaDB({
  useHNSW: true,
  hnsw: { M: 32 },
});

M Impact

M ValueBuild SpeedMemoryRecall
8FastLow~93%
16MediumMedium~96%
32SlowHigh~98%

efConstruction

Controls build-time quality. Higher values = better graph structure but slower indexing.
src/hnsw-ef-construction.ts
// Fast construction, acceptable quality
const quickBuild = new VectoriaDB({
  useHNSW: true,
  hnsw: { efConstruction: 100 },
});

// Slow construction, excellent quality
const highQuality = new VectoriaDB({
  useHNSW: true,
  hnsw: { efConstruction: 400 },
});

efConstruction Impact

ValueBuild SpeedIndex Quality
100FastGood
200MediumVery Good
400SlowExcellent

efSearch

Controls search-time quality. Can be adjusted per-query:
src/hnsw-ef-search.ts
const db = new VectoriaDB({
  useHNSW: true,
  hnsw: { efSearch: 50 }, // Default
});

// High-precision search
const results = await db.search(query, {
  topK: 10,
  efSearch: 200, // Override for this query
});

// Fast search (lower recall)
const quick = await db.search(query, {
  topK: 10,
  efSearch: 20,
});

efSearch Impact

ValueSearch SpeedRecall
20Very Fast~92%
50Fast~96%
100Medium~98%
200Slow~99%

Configuration Presets

src/hnsw-presets.ts
// Speed-optimized (95%+ recall)
const speedOptimized = {
  M: 12,
  efConstruction: 100,
  efSearch: 30,
};

// Balanced (97%+ recall)
const balanced = {
  M: 16,
  efConstruction: 200,
  efSearch: 50,
};

// Quality-optimized (99%+ recall)
const qualityOptimized = {
  M: 32,
  efConstruction: 400,
  efSearch: 100,
};

Full Configuration Example

src/hnsw-full-config.ts
const db = new VectoriaDB<ToolDocument>({
  useHNSW: true,
  hnsw: {
    M: 16,              // Connections per node
    M0: 32,             // Layer 0 connections (optional)
    efConstruction: 200, // Build quality
    efSearch: 50,       // Search quality
  },
  maxDocuments: 100000,
  maxBatchSize: 1000,
});

HNSW Overview

Introduction to HNSW

HNSW Tuning

Tune for your use case

Search Performance

General performance tips