Overview
TheWasmNltk class provides a WebAssembly-based runtime for bun_nltk operations. It offers cross-platform compatibility and can run in browsers, Bun, Node.js, and other JavaScript environments.
Static Methods
WasmNltk.init()
Initializes a new WebAssembly NLTK instance.Parameters
Initialization options for the WASM runtime.
WasmNltkInit Type
Pre-loaded WebAssembly binary bytes. If provided, the WASM module will be instantiated from these bytes instead of loading from the filesystem.
Custom path to the WASM file. Defaults to
../native/bun_nltk.wasm relative to the module directory.Returns
Returns aPromise<WasmNltk> that resolves to an initialized WASM NLTK instance.
Example
Instance Methods
dispose()
Frees all allocated memory blocks and cleans up the WASM instance.Example
Memory Management
The WASM runtime uses an internal memory pool system:- Input Buffer: Fixed-size buffer for text input (capacity determined at compile time)
- Memory Blocks: Dynamically allocated blocks for outputs (offsets, lengths, metrics, etc.)
- Automatic Reuse: Blocks are reused across operations when possible
Input Buffer Capacity
The WASM runtime has a fixed input buffer capacity. If you try to process text larger than this capacity, an error will be thrown:Error Handling
The WASM runtime checks for errors after each operation. If an error occurs, it will throw with a descriptive message:Best Practices
- Reuse Instances: Create one
WasmNltkinstance and reuse it for multiple operations - Memory Cleanup: Always call
dispose()when done, especially in long-running applications - Input Size: Be aware of input buffer limitations for very large texts
- Browser Usage: In browsers, consider lazy-loading the WASM module on demand
Platform Compatibility
The WASM runtime works across all JavaScript environments:- Bun: Native support
- Node.js: Requires Node 16+ with WebAssembly support
- Browsers: Modern browsers with WebAssembly support
- Deno: Full compatibility
- Edge runtimes: Cloudflare Workers, Vercel Edge Functions, etc.
See Also
- WASM Methods - All available WASM instance methods
- Native APIs - Native library alternative