k-d Tree JavaScript Library
The `kd-tree-javascript` library provides a basic but high-performance JavaScript implementation of the k-dimensional tree data structure. It's designed for organizing points in k-dimensional space, facilitating efficient range searches and nearest neighbor queries. Currently at version 1.0.3, the library uses a UMD (Universal Module Definition) pattern, allowing it to be used in browser environments (exposing global variables `kdTree` and `BinaryHeap`) and with module loaders like RequireJS. Its primary differentiator was its reported speed and simplicity for specific spatial data operations, as highlighted by various demos. Due to its last update being in 2017, the library is considered abandoned, meaning no further feature development, bug fixes, or security patches are expected.
Common errors
-
ReferenceError: kdTree is not defined
cause The `kdTree` global variable was not loaded, likely due to an incorrect script path, missing `<script>` tag in HTML, or incorrect module import.fixEnsure the `kdTree.js` file is correctly linked in your HTML before other scripts using it, or use `require()` with the correct module path in a CommonJS environment. -
TypeError: Cannot read properties of undefined (reading 'kdTree')
cause Occurs in CommonJS/RequireJS environments when the module is not correctly imported and assigned, or the property `kdTree` is accessed on an undefined module export.fixFor CommonJS, use `const ubilabs = require('kd-tree-javascript');` and then `new ubilabs.kdTree(...)`. For RequireJS, ensure the module path is correct and the callback argument (e.g., `ubilabs`) is used as shown in the documentation.
Warnings
- gotcha When loaded directly in a browser via a `<script>` tag, `kdTree` and `BinaryHeap` are exposed as global variables, which may conflict with other libraries or application code using the same names.
- gotcha The library has not been updated since October 2017, meaning there will be no further development, bug fixes, or security patches. Users should be aware of potential vulnerabilities or compatibility issues with newer JavaScript runtimes, browser versions, or evolving web standards.
- gotcha The k-d tree structure can become unbalanced with frequent insertions and deletions, leading to degraded query performance (approaching O(N) instead of O(log N)). The library does not provide automatic rebalancing.
Install
-
npm install kd-tree-javascript -
yarn add kd-tree-javascript -
pnpm add kd-tree-javascript
Imports
- kdTree (global)
import { kdTree } from 'kd-tree-javascript';<!-- In HTML --> <script src="path/to/kdTree.js"></script> <script> const tree = new kdTree(points, distance, dimensions); </script>
- kdTree (CommonJS)
const { kdTree } = require('kd-tree-javascript');const ubilabs = require('kd-tree-javascript'); const tree = new ubilabs.kdTree(points, distance, dimensions); - kdTree (RequireJS / AMD)
const tree = new kdTree(points, distance, dimensions); // if not globally defined
requirejs(['path/to/kdTree.js'], function (ubilabs) { const tree = new ubilabs.kdTree(points, distance, dimensions); });
Quickstart
var points = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6},
{x: 7, y: 8}
];
var distance = function(a, b){
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
}
// Assuming kdTree is globally available or imported via CommonJS/AMD as 'ubilabs'
// For global: new kdTree(points, distance, ["x", "y"]);
// For CommonJS: new ubilabs.kdTree(points, distance, ["x", "y"]);
// For this example, we'll assume global/browser context as in README.
var tree = new kdTree(points, distance, ["x", "y"]);
var nearest = tree.nearest({ x: 5, y: 5 }, 2);
console.log(nearest);
// Expected output:
// [ [ { x: 5, y: 6 }, 1 ], [ { x: 7, y: 8 }, 8 ] ]
// (point {x:5,y:6} at distance 1, point {x:7,y:8} at distance 8)