npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ferrow/embeddings-similarity

v1.0.0

Published

Vector math and in-memory embedding search — cosine/dot/euclidean, normalization, heap-based top-K, batch top-K, seeded k-means-lite clustering. Zero runtime dependencies.

Downloads

79

Readme

embeddings-similarity

CI

Vector math and in-memory embedding search — cosine/dot/euclidean, normalization, heap-based top-K, batch top-K, seeded k-means-lite clustering. Zero runtime dependencies, strict TypeScript.

Quickstart

import { cosineSimilarity, topK, kmeansLite } from "embeddings-similarity";

cosineSimilarity([1, 0, 0], [0.9, 0.1, 0]); // => 0.9939...

const vectors = [
  { vector: [1, 0, 0], metadata: { id: "east" } },
  { vector: [0, 1, 0], metadata: { id: "north" } },
  { vector: [0.95, 0.05, 0], metadata: { id: "near-east" } },
];

topK([1, 0, 0], vectors, 2, "cosine");
// => [{ index: 0, score: 1, metadata: {...} }, { index: 2, score: 0.998..., metadata: {...} }]

kmeansLite([[0, 0], [0.1, 0.1], [10, 10], [10.1, 9.9]], { k: 2, seed: 7 });
// => { centroids: [...], assignments: [...] }

API

  • cosineSimilarity(a, b): number — in [-1, 1]; returns 0 if either vector has zero magnitude.
  • dot(a, b): number — raw dot product.
  • euclideanDistance(a, b): number — L2 distance.
  • magnitude(a): number — L2 norm.
  • normalize(a): Vector — unit-length copy; zero vectors returned unchanged.
  • topK(query, vectors, k, metric?): TopKResult[]metric is "cosine" (default), "dot", or "euclidean". Uses a bounded min-heap of size k, not a full sort.
  • batchTopK(queries, vectors, k, metric?): TopKResult[][]topK for each query.
  • kmeansLite(vectors, options): KMeansResultoptions: { k, iterations?, seed? }. Fixed iteration count (default 10), seeded deterministic init (default seed 42).
  • class DimensionMismatchError extends Error — thrown by any comparison given vectors of different lengths.

Types

type Vector = number[];
type SimilarityMetric = "cosine" | "dot" | "euclidean";
interface VectorEntry<T> { vector: Vector; metadata?: T; }
interface TopKResult<T> { index: number; score: number; metadata?: T; }
interface KMeansOptions { k: number; iterations?: number; seed?: number; }
interface KMeansResult { centroids: Vector[]; assignments: number[]; }

Limits

  • Brute-force exact search. topK/batchTopK scan every candidate vector — fine up to roughly 100k vectors on a single machine, but there is no indexing (HNSW, IVF, etc). Beyond that scale, use a real vector database.
  • kmeansLite runs a fixed iteration count with no convergence check — it is for quick exploratory clustering, not a tuned clustering library. Empty clusters keep their previous centroid rather than reseeding.
  • All vector-pair functions throw DimensionMismatchError on length mismatch rather than silently truncating or padding.

Part of the ferrow-toolkit collection · Sponsored by Ferrow