{"id":16221,"library":"snappyjs","title":"SnappyJS","description":"SnappyJS is a pure JavaScript implementation of Google's Snappy compression algorithm, designed for both Node.js and browser environments. The current stable version is 0.7.0. While there isn't an explicit, regular release cadence, updates appear to be driven by feature additions and maintenance, as indicated by the recent minor version bumps (e.g., 0.7.0, 0.6.1, 0.5.0). Its primary differentiator is being entirely written in JavaScript, leveraging `ArrayBuffer` for efficient handling of binary data. This allows it to run without native dependencies, making it highly suitable for web applications and environments where native modules might be problematic or undesirable. Benchmarks suggest that it achieves approximately 35%-45% of the performance of native Snappy implementations for larger inputs in Node.js, though it can occasionally outperform native solutions for very small input sizes due to lower FFI (Foreign Function Interface) overhead. It's a reliable choice for client-side compression needs or Node.js projects prioritizing pure JS solutions.","status":"active","version":"0.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/zhipeng-jia/snappyjs","tags":["javascript","snappy"],"install":[{"cmd":"npm install snappyjs","lang":"bash","label":"npm"},{"cmd":"yarn add snappyjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add snappyjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For CommonJS environments, use 'const SnappyJS = require(\"snappyjs\")'. The library primarily exports a single object containing 'compress' and 'uncompress' methods.","wrong":"import { SnappyJS } from 'snappyjs'","symbol":"SnappyJS","correct":"import SnappyJS from 'snappyjs'"},{"note":"The 'compress' function is a method of the main SnappyJS object. Ensure you import the default SnappyJS object first.","wrong":"import { compress } from 'snappyjs'","symbol":"compress","correct":"import SnappyJS from 'snappyjs'; const compressed = SnappyJS.compress(input);"},{"note":"The 'uncompress' function is a method of the main SnappyJS object. It supports an optional 'maxLength' parameter for security since v0.7.0.","wrong":"import { uncompress } from 'snappyjs'","symbol":"uncompress","correct":"import SnappyJS from 'snappyjs'; const uncompressed = SnappyJS.uncompress(compressed, maxLength);"}],"quickstart":{"code":"import * as SnappyJS from 'snappyjs';\n\n// Example data: a simple text string converted to a Uint8Array\nconst text = \"Hello, SnappyJS! This is a test string to demonstrate compression and decompression.\";\nconst encoder = new TextEncoder();\nconst originalData = encoder.encode(text);\n\n// SnappyJS expects ArrayBuffer, Buffer, or Uint8Array. \n// Uint8Array's .buffer property provides the underlying ArrayBuffer.\nconst inputBuffer = originalData.buffer;\n\nconsole.log(`Original text: \"${text}\"`);\nconsole.log('Original size:', inputBuffer.byteLength, 'bytes');\n\ntry {\n  // Compress the data\n  const compressed = SnappyJS.compress(inputBuffer);\n  console.log('Compressed size:', compressed.byteLength, 'bytes');\n\n  // Uncompress the data\n  // Using maxLength as a security measure, estimating twice the original size as a safe upper bound.\n  const estimatedMaxLength = inputBuffer.byteLength * 2;\n  const uncompressed = SnappyJS.uncompress(compressed, estimatedMaxLength);\n  console.log('Uncompressed size:', uncompressed.byteLength, 'bytes');\n\n  // Convert back to string to verify\n  const decoder = new TextDecoder();\n  const decompressedText = decoder.decode(uncompressed);\n  console.log('Decompressed text:', decompressedText);\n\n  if (text === decompressedText) {\n    console.log(\"Compression and decompression successful and data integrity maintained!\");\n  } else {\n    console.error(\"Error: Data mismatch after compression/decompression!\");\n  }\n} catch (error) {\n  console.error(\"An error occurred during SnappyJS operation:\", error.message);\n}","lang":"javascript","description":"Demonstrates how to import SnappyJS, convert a string to an `ArrayBuffer`, then compress and uncompress the data, including verification and using the `maxLength` security feature."},"warnings":[{"fix":"Add `node: { Buffer: false }` to your webpack configuration object to prevent automatic `Buffer` polyfilling.","message":"When bundling `snappyjs` for browser environments using webpack, explicitly disable the Node.js `Buffer` polyfill to prevent unnecessary code from being included in your bundle. This is crucial if you intend to only use `ArrayBuffer` or `Uint8Array` as input parameters.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Evaluate your application's performance needs; use native Snappy bindings (e.g., `node-snappy`) for maximum speed in Node.js, or accept the pure JavaScript performance for browser compatibility and simpler deployments.","message":"While SnappyJS is optimized JavaScript, its performance for larger inputs (e.g., >100KB) typically reaches only 35-45% of native Snappy implementations. For extremely performance-critical Node.js applications, consider native C++ bindings if cross-platform pure JS isn't a strict requirement.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always call `SnappyJS.uncompress(compressedData, expectedMaxLength)` and provide a reasonable upper bound for the uncompressed data size based on your application's expectations.","message":"Since v0.7.0, the `uncompress` function accepts an optional `maxLength` parameter. It is highly recommended to provide this parameter as a protection mechanism against malicious or malformed data streams that could attempt to exhaust memory by declaring an excessively large uncompressed size.","severity":"gotcha","affected_versions":">=0.7.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure you are importing the default export (e.g., `import SnappyJS from \"snappyjs\"`) or correctly requiring the module (e.g., `const SnappyJS = require(\"snappyjs\")`) before calling its methods.","cause":"The SnappyJS module was not correctly imported or required, or the 'compress' method was called on an undefined object.","error":"TypeError: Cannot read properties of undefined (reading 'compress')"},{"fix":"Add `node: { Buffer: false }` to your webpack configuration object to explicitly prevent `Buffer` polyfilling, especially if you only intend to use `ArrayBuffer` or `Uint8Array`.","cause":"When bundling for a browser environment with webpack, the Node.js `Buffer` global is not available, and webpack may implicitly attempt to polyfill it, failing if not configured correctly.","error":"ReferenceError: Buffer is not defined"},{"fix":"Convert your data into an `ArrayBuffer`, `Buffer` (Node.js only), or `Uint8Array` before passing it to SnappyJS compression/decompression functions.","cause":"The input provided to `SnappyJS.compress` or `SnappyJS.uncompress` is not one of the supported binary data types.","error":"Error: Input must be type of ArrayBuffer, Buffer, or Uint8Array"},{"fix":"Verify the integrity of your compressed data. If using `uncompress(data, maxLength)`, ensure `maxLength` is set appropriately to prevent unexpected data sizes from causing an error.","cause":"This error typically occurs during decompression if the input `compressed` data stream is corrupted, malformed, or does not conform to the Snappy format. It can also be triggered if the `maxLength` parameter in `uncompress` is exceeded by the data's internal header.","error":"RangeError: Offset is out of bounds"}],"ecosystem":"npm"}