WebAssembly Platform
JSON Packer provides high-performance WebAssembly bindings suitable for browser and Node.js environments.
Version Update (v0.1.1)
- Size Optimization: WASM package size optimized from 165KB to 112KB, a 32.1% reduction. JavaScript glue code compression enabled.
Installation
bash
npm install json-packer-wasm
# or
yarn add json-packer-wasm
# or
pnpm add json-packer-wasm
API
Core Functions
typescript
// Compress to byte array
export function compress_to_bytes(jsonString: string, options: Options): Uint8Array;
// Compress to Base64 string
export function compress_to_base64(jsonString: string, options: Options): string;
// Decompress from byte array
export function decompress_from_bytes(bytes: Uint8Array): string;
// Decompress from Base64 string
export function decompress_from_base64(base64: string): string;
Configuration Options
typescript
export class Options {
constructor(
enable_value_pool: boolean,
pool_min_repeats: number,
pool_min_string_len: number
);
}
Examples
Browser (ES Modules)
javascript
import init, { Options, compress_to_base64, decompress_from_base64 } from 'json-packer-wasm';
async function example() {
// Initialize WASM module
await init();
const data = { name: "Alice", age: 30, active: true };
const jsonStr = JSON.stringify(data);
// Create compression options
const options = new Options(false, 3, 8); // Disable value pool
// Compress
const compressed = compress_to_base64(jsonStr, options);
console.log('Compressed:', compressed);
// Decompress
const decompressed = decompress_from_base64(compressed);
const restored = JSON.parse(decompressed);
console.log('Restored:', restored);
}
example();
Enable String Value Pool
javascript
import init, { Options, compress_to_bytes, decompress_from_bytes } from 'json-packer-wasm';
await init();
const data = {
logs: [
{ level: "info", message: "server started successfully" },
{ level: "info", message: "server started successfully" },
{ level: "error", message: "connection timeout occurred" },
{ level: "error", message: "connection timeout occurred" }
]
};
// Enable value pool: strings repeated 2+ times with length >= 6 enter value pool
const options = new Options(true, 2, 6);
const compressed = compress_to_bytes(JSON.stringify(data), options);
const restored = JSON.parse(decompress_from_bytes(compressed));
console.log('Original size:', new TextEncoder().encode(JSON.stringify(data)).length);
console.log('Compressed size:', compressed.length);