UNPKG

@sroussey/simsimd

Version:

Fastest SIMD-Accelerated Vector Similarity Functions for x86 and Arm

147 lines (146 loc) 5.51 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.jensenshannon = exports.kullbackleibler = exports.jaccard = exports.hamming = exports.inner = exports.cosine = exports.sqeuclidean = void 0; const node_gyp_build_1 = __importDefault(require("node-gyp-build")); const path = __importStar(require("node:path")); const node_fs_1 = require("node:fs"); const bindings_1 = require("bindings"); const fallback = __importStar(require("./fallback.js")); let compiled; try { let builddir = getBuildDir(getDirName()); compiled = (0, node_gyp_build_1.default)(builddir); } catch (e) { compiled = fallback; console.warn("It seems like your environment does't support the native simsimd module, so we are providing a JS fallback."); } /** * @brief Computes the squared Euclidean distance between two vectors. * @param {Float32Array|Int8Array} a - The first vector. * @param {Float32Array|Int8Array} b - The second vector. * @returns {number} The squared Euclidean distance between vectors a and b. */ const sqeuclidean = (a, b) => { return compiled.sqeuclidean(a, b); }; exports.sqeuclidean = sqeuclidean; /** * @brief Computes the cosine distance between two vectors. * @param {Float32Array|Int8Array} a - The first vector. * @param {Float32Array|Int8Array} b - The second vector. * @returns {number} The cosine distance between vectors a and b. */ const cosine = (a, b) => { return compiled.cosine(a, b); }; exports.cosine = cosine; /** * @brief Computes the inner product of two vectors. * @param {Float32Array} a - The first vector. * @param {Float32Array} b - The second vector. * @returns {number} The inner product of vectors a and b. */ const inner = (a, b) => { return compiled.inner(a, b); }; exports.inner = inner; /** * @brief Computes the bitwise Hamming distance between two vectors. * @param {Uint8Array} a - The first vector. * @param {Uint8Array} b - The second vector. * @returns {number} The Hamming distance between vectors a and b. */ const hamming = (a, b) => { return compiled.hamming(a, b); }; exports.hamming = hamming; /** * @brief Computes the bitwise Jaccard similarity coefficient between two vectors. * @param {Uint8Array} a - The first vector. * @param {Uint8Array} b - The second vector. * @returns {number} The Jaccard similarity coefficient between vectors a and b. */ const jaccard = (a, b) => { return compiled.jaccard(a, b); }; exports.jaccard = jaccard; /** * @brief Computes the kullbackleibler similarity coefficient between two vectors. * @param {Float32Array} a - The first vector. * @param {Float32Array} b - The second vector. * @returns {number} The Jaccard similarity coefficient between vectors a and b. */ const kullbackleibler = (a, b) => { return compiled.kullbackleibler(a, b); }; exports.kullbackleibler = kullbackleibler; /** * @brief Computes the jensenshannon similarity coefficient between two vectors. * @param {Float32Array} a - The first vector. * @param {Float32Array} b - The second vector. * @returns {number} The Jaccard similarity coefficient between vectors a and b. */ const jensenshannon = (a, b) => { return compiled.jensenshannon(a, b); }; exports.jensenshannon = jensenshannon; exports.default = { sqeuclidean: exports.sqeuclidean, cosine: exports.cosine, inner: exports.inner, hamming: exports.hamming, jaccard: exports.jaccard, kullbackleibler: exports.kullbackleibler, jensenshannon: exports.jensenshannon, }; // utility functions to help find native builds function getBuildDir(dir) { if ((0, node_fs_1.existsSync)(path.join(dir, "build"))) return dir; if ((0, node_fs_1.existsSync)(path.join(dir, "prebuilds"))) return dir; if (path.basename(dir) === ".next") { // special case for next.js on custom node (not vercel) const sideways = path.join(dir, "..", "node_modules", "simsimd"); if ((0, node_fs_1.existsSync)(sideways)) return getBuildDir(sideways); } if (dir === "/") throw new Error("Could not find native build for simsimd"); return getBuildDir(path.join(dir, "..")); } function getDirName() { try { if (__dirname) return __dirname; } catch (e) { } return (0, bindings_1.getRoot)((0, bindings_1.getFileName)()); }