A Hierarchical Navigable Small World index built in ~240 lines of dependency-free JavaScript (hnsw.mjs). Vectors live in ℝd; the canvas is a fixed random projection down to 2-D. The scorecard is a 40-query held-out evaluation — HNSW's top-k against a brute-force scan — recomputed on every change, so the recall you trade for the speed-up is measured, not asserted. Push dimensions up, or lean out the graph with M / efConstruction, and watch recall fall; widen efSearch to buy it back at the cost of more distance calls. Click the canvas to aim the query the animation traces.
floor(-ln(U) · mL)). Most nodes live only on layer 0; a few reach
higher, and the highest is the entry point. Use the buttons to view one layer's edges at a time.
efConstruction finds candidates, then a diversity heuristic keeps up to
M of them — preferring neighbours that are closer to the new node than to each
other, which keeps the graph navigable instead of clumped.
efSearch. Cyan rings trace the visit order.
efSearch to buy back recall at the cost of more work.
dimensions rises the greedy walk has a harder time routing to the true
neighbourhood, so recall drops at a fixed efSearch. Graph quality:
M and efConstruction set how well-connected the graph is — a
generous graph (M ≈ 16, efConstruction ≈ 64) is
near-exact at a modest efSearch; a lean one needs a much wider beam to catch up.
The canvas is only a shadow of ℝd — every distance, every score and the recall run
on the real vectors, not the projection.
The same BM25 / RRF / cosine / metrics code, written the same way, powers Retrieval Playground and MELAI Engineering Lab.