vector search nodejs
Vector Search in Node.js — Server-Side Similarity
Node.js developers usually hit the same search problem: keyword search is easy to ship, but it fails when the user phrase and the document phrase do not overlap. altor-vec solves the retrieval side by running HNSW vector search locally in WebAssembly. That means you can keep query latency close to the browser, eliminate per-query billing, and still expose a familiar framework component API. This guide focuses on implementation details rather than marketing claims.
npm install altor-vecThe example below uses tiny four-dimensional vectors so the code is runnable as-is and easy to understand. In production you would usually replace those manual vectors with embeddings from a model such as Xenova/all-MiniLM-L6-v2 or a build-time embedding job. The retrieval flow stays the same: install, import the WASM package, create an index, optionally add vectors, then query the engine and map result IDs back to metadata.
Step 1: install and understand the runtime boundary
Start with npm install altor-vec. The package exposes a default init() function that loads the WASM module and a WasmSearchEngine class that loads or builds an HNSW index. The important design question in Node.js is not whether vector retrieval is possible. It is where initialization should live so the index is created once, memory is released intentionally, and queries do not trigger unnecessary work on each re-render or navigation.
Step 2: import the library and create the index
The sample builds an index from a flat Float32Array. That matches the real API from the package README: WasmSearchEngine.from_vectors(flat, dims, m, ef_construction, ef_search). The four HNSW parameters here are conservative defaults for a small browser index. If you precompute a production index offline, you can instead serialize it with to_bytes() and load it using new WasmSearchEngine(bytes).
import init, { WasmSearchEngine } from 'altor-vec';
import http from 'node:http';
const docs = [
{ id: 'doc-1', title: 'Node workers', vector: [1, 0, 0, 0] },
{ id: 'doc-2', title: 'HTTP caching', vector: [0, 1, 0, 0] },
{ id: 'doc-3', title: 'Browser RAG gateway', vector: [0, 0, 1, 0] },
];
await init();
const dim = 4;
const flat = new Float32Array(docs.flatMap((doc) => doc.vector));
const engine = WasmSearchEngine.from_vectors(flat, dim, 16, 200, 50);
docs.push({ id: 'doc-4', title: 'Streaming SSR', vector: [0.94, 0.06, 0, 0] });
engine.add_vectors(new Float32Array([0.94, 0.06, 0, 0]), dim);
const server = http.createServer((req, res) => {
if (req.url !== '/search') {
res.writeHead(404).end();
return;
}
const queryVector = new Float32Array([0.95, 0.05, 0, 0]);
const hits: [number, number][] = JSON.parse(engine.search(queryVector, 3));
const payload = hits.map(([id, distance]) => ({ ...docs[id], distance }));
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(payload));
});
server.listen(3000, () => {
console.log('Vector search running on http://localhost:3000/search');
});Step 3: what the code is actually doing
- Install: the project adds
altor-vecfrom npm. - Import: the code imports
initandWasmSearchEngine. - Create index: manual vectors are flattened into a single
Float32Arrayand passed intofrom_vectors(). - Add vectors: the example appends one more vector via
add_vectors()so you can see incremental updates. - Query: it converts a query vector into a
Float32Array, callssearch(), parses the JSON response, and maps IDs back to the in-memory document array.
That pattern is stable across browser frameworks because altor-vec is model-agnostic. The framework concerns are mostly lifecycle-related: where to hold the engine instance, how to debounce query creation, and whether embeddings run on the main thread or in a worker. If you keep those concerns separate, semantic retrieval feels surprisingly ordinary.
Performance notes specific to this framework
- Node removes browser memory pressure, but the same published retrieval baseline still applies because altor-vec is the same WASM core.
- Use worker threads or process-level isolation if you rebuild indexes frequently; query latency is tiny, build work is the heavy step.
- Server-side retrieval simplifies shared caches and authorization, but you reintroduce network latency that client-side search avoids.
0.6ms, index load is about 19ms, the raw WASM binary is 117KB, and the gzipped WASM payload is 54KB. In real apps, embedding generation and rendering usually cost much more than the vector lookup itself.When to use client-side vs server-side in Node.js
Client-side: Use Node when search must run close to protected data, when many clients should share one index, or when you need to inject filters on the server.
Server-side: Use client-side retrieval instead when the corpus is public and you want zero round trips plus no backend query cost.
A good rule is simple. If the data is already safe to send to every browser and you mostly care about fast semantic ranking, keep it local. If the search layer also needs to enforce business rules, security boundaries, or complex shared state, put retrieval on the server and let Node.js call it as a normal endpoint.
Production checklist
- Cache the WASM and serialized index aggressively with versioned asset names.
- Validate vector dimensions before every search to prevent subtle runtime errors.
- Keep metadata outside the HNSW graph so result rendering stays flexible.
- Measure cold start, repeated search latency, and memory on at least one mid-range mobile device.
- Free the engine explicitly if you unload large indexes on navigation.
Conclusion
Node.js does not require a special semantic-search abstraction. It only needs a clean place to initialize the engine and a disciplined boundary between embedding, retrieval, and UI state. altor-vec gives you a small browser-native ANN core, while the framework handles rendering and ergonomics. If you want a developer-friendly starting point with no backend dependency, this is the shortest path: npm install altor-vec, build or load an index, and search locally.
Related hubs: Framework guides · Use cases · Comparison pages