# hnsw-wasm

> Emnapi generated WebAssembly bindings for Hnswlib

Latest version **1.0.0** (published 2023-04-12) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install hnsw-wasm
pnpm add hnsw-wasm
yarn add hnsw-wasm
bun add hnsw-wasm
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-04-12 |
| First published | 2023-04-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 1 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Kaio Barbosa-Chifan |
| Maintainers | kaiobarb |
| Keywords | hnswlib, wasm, WebAssembly, emnapi, emscripten, bindings |

## Links

- npm: https://www.npmjs.com/package/hnsw-wasm
- npm.io page: https://npm.io/package/hnsw-wasm

## Dependencies (2)

- [hnswlib-wasm](https://npm.io/package/hnswlib-wasm.md) ^0.0.5-beta.1
- [@emnapi/runtime](https://npm.io/package/@emnapi/runtime.md) ^0.36.3

## Recent versions

- 1.0.0 (latest) — 2023-04-12

## README

# hnswlib-wasm

`hnswlib-wasm` is a WebAssembly (WASM) binding for the HNSWLIB library, built on top of [hnswlib-node](https://github.com/yoshoku/hnswlib-node) using [Emnapi](https://github.com/toyobayashi/emnapi).


## Installation

Install `hnswlib-wasm` using npm:

```bash
npm install hnswlib-wasm
```

## Usage

Here's an example of how to use the `hnswlib-wasm` library:

```javascript
import {getDefaultContext} from '@emnapi/runtime';
import hnswlibModule from 'hnswlib-wasm';

(async () => {
  const Module = await hnswlibModule();
  const hnswlib = Module.emnapiInit({ context: emnapi.getDefaultContext() });
  const { HierarchicalNSW } = hnswlib;

  const numDimensions = 5;
  const maxElements = 1000;

  const index = new HierarchicalNSW('l2', numDimensions);
  index.initIndex(maxElements, 16, 200, 100);

  index.addPoint([0, 1, 2, 3, 4], 0);
  index.addPoint([1, 2, 3, 4, 5], 1);
  index.addPoint([3, 4, 5, 6, 6], 2);

  const numNeighbors = 3;
  const result = index.searchKnn([1, 4, 2, 3, 4], numNeighbors);
  console.log(result);
})();
```
## API
Since this library is built on top of [hnswlib-node](https://github.com/yoshoku/hnswlib-node), the api is exactly the same as those listed [here](https://yoshoku.github.io/hnswlib-node/doc/). The only difference in usage is the presence of the web assembly module. Using the web assembly module is illustrated in the example given above.

## Building the library
You will need:
- emscripten `>= 3.1.9`
- cmake
- emnapi

To build the library, you can use the following npm script:

```
npm run build
```

This will execute the necessary CMake commands to build the WASM module.
Tests

To run the tests, simply execute:

```bash
npm test
```

## License

hnswlib-wasm is licensed under the Apache-2.0 License.

## Acknowledgements
These bindings were created on top of the [hnswlib-node](https://github.com/yoshoku/hnswlib-node) package.
The [emnapi](https://github.com/toyobayashi/emnapi) library was invaluable in the creation of these bindings.

---
_Source: https://npm.io/package/hnsw-wasm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
