
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
Javascript implementation of the Vantage-Point Tree nearest neighbor search algorithm
A JavaScript implementation of the Vantage-Point Tree nearest neighbor search algorithm.
The VP tree is particularly useful in dividing data in a non-standard metric space into a BSP tree. Tree construction executes in O(n log(n)) time, and search is under certain circumstances and in the limit, O(log(n)) expected time. This makes it suitable when distance computations are expensive.
// A whole lot of strings
var stringList = [
'culture',
'democracy',
'metaphor',
'irony',
'hypothesis',
'science',
'fastuous',
'integrity',
'synonym',
'empathy' // and on and on...
];
// building the tree
vptree = VPTreeFactory.build(stringList, levenshteinDistance);
nearest = vptree.search('democratic'); // [{"i":1,"d":3}]
index = nearest[0].i; // index of nearest element is 1
distance = nearest[0].d; // distance of nearest element is 3
alert( stringList[index] ); // alerts 'democracy'
The API exposes the VPTreeFactory object.
VPTreeFactory.build(S, distance[, bucketSize])
Builds a fresh VPTree instance.
VPTreeFactory.select(list, k, comp)
An implementation of the quick select algorithm like the nth_element function of the Standard C++ Library.
You will probably never use this function. However, it is used internally, and exposed as a bonus. Could be useful. Who knows.
vptree.search(element[, n])
Searches the n nearest neighbors of element in S.
This function returns the list of the n nearest elements found, ordered from the closest to the furthest. Each item in the list is an object with 2 properties :
Typical usage of this library involves large datasets or expensive distance computations. You will probably want to precompute the vp-tree structure, so that your final application does just the searching.
vptree.stringify()
Returns a stringified JavaScript object literal of the vp-tree structure. Like JSON.stringify but without nulls and quotes to save space. It is valid JavaScript, but not valid JSON, so JSON.parse() will complain.
The stringified object is not the whole VPTree instance : it does not contain the initial dataset, nor the
distance function. Its only purpose is to be pasted in the code of your final app, where it will have to
be turned back into a searchable VPTree instance with the load() function.
VPTreeFactory.load(S, distance, tree)
Reuses a precomputed stringified vp-tree, and returns a searchable VPTree instance.
The vp-tree algorithm needs a real metric. In particular, the squared euclidean distance won't do the job because it does not satisfy the triangle inequality : if you want to use the standard euclidean distance, don't forget the square root.
FAQs
Javascript implementation of the Vantage-Point Tree nearest neighbor search algorithm
We found that vptree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.