{"library":"snappyjs","title":"SnappyJS","type":"library","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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install snappyjs"],"cli":null},"imports":["import SnappyJS from 'snappyjs'","import SnappyJS from 'snappyjs'; const compressed = SnappyJS.compress(input);","import SnappyJS from 'snappyjs'; const uncompressed = SnappyJS.uncompress(compressed, maxLength);"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/zhipeng-jia/snappyjs","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/snappyjs","openapi_spec":null,"status_page":null,"smithery":null},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}