Use Kwker from JavaScript
Two builds share one API. The npm package kwker holds a Node-API addon for Node.js and Bun (prebuilt for linux-x64,
linux-arm64, darwin-arm64 and win32-x64) with TypeScript declarations; it sorts TypedArrays in place. In browsers,
Deno, or anywhere without native addons, the WebAssembly build (kwker.wasm with kwker.mjs) runs the same calls.
Setup
npm install kwker
The program
The three slowest requests, without sorting the whole array (index.js):
const kwk = require("kwker");
const latencyMs = new Float64Array([12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0]);
const { values, indices } = kwk.topK(latencyMs, 3, { descending: true });
console.log(values);
console.log(indices);
Float64Array(3) [ 310.2, 260, 250.4 ]
Uint32Array(3) [ 4, 6, 1 ]
Run
node index.js
For the browser, see WebAssembly: it loads the module and copies each array into its memory and back.
Errors
Arrays that are not a supported TypedArray throw TypeError, and k out of range throws RangeError; the arrays are
left unchanged.
Next steps
- JavaScript API reference: every function and its options.
- Quickstart: Kwker Core: sorting, order and medians in every language.