UNPKG

@krishnadaspc/tiny-json

Version:

Lightweight JSON compressor** with reversible key/value renaming, built for API response optimization and payload shrinking.

186 lines (126 loc) โ€ข 4.93 kB
# ๐Ÿ“ฆ tiny-json **Lightweight JSON compressor** with reversible key/value renaming, built for API response optimization and payload shrinking. --- ## ๐Ÿš€ Why tiny-json? Most JSON payloads in APIs repeat the same keys and values over and over. `tiny-json` compresses that structure by: * Renaming long keys to short ones * Replacing repeated values with short tokens * Preserving full reversibility (no schema needed) * Providing size savings even before GZIP --- ## โš ๏ธ When NOT to Use This library is not suitable for: * Very small JSON (less than 1KB) * Highly unique or non-repetitive data * Cases where every byte must be fully human-readable For those, GZIP alone is better. --- ## โœ… Best Use Cases * API responses with repeated records or nested lists * Reversible frontend-backend JSON sync * Paginated lists with uniform object structures * Storing JSON in limited-space environments --- ## โœจ Features * ๐Ÿ” Replaces long keys with short tokens * ๐Ÿ”„ Optional repeated value deduplication * ๐Ÿ“‰ `analyze()` gives original vs compressed size * ๐Ÿ”ฌ Deep nested JSON support * ๐Ÿ“ฆ `benchmark.js` for real-world API testing * ๐Ÿงฉ **Zero external dependencies** โ€“ works anywhere, fast and lightweight --- ## โ˜๏ธ Cloud Bandwidth Cost Savings with `tiny-json` ### ๐Ÿ“ฆ Assumptions - You serve **10 million API requests** monthly - Each raw JSON response is **100 KB** - Bandwidth usage = **1 TB/month** | Provider | Avg Egress Cost / GB | Monthly Cost (1 TB) | With `tiny-json` (50% less) | Savings/Month | |-----------------------|-----------------------|----------------------|------------------------------|----------------| | **Cloudflare** | **$0.00โ€“$0.09** | ~$0โ€“$90 | ~$0โ€“$45 | **Up to $45** | | **AWS CloudFront** | $0.085 | ~$85 | ~$42.5 | **$42.5** | | **Google Cloud CDN** | $0.08โ€“$0.12 | ~$80โ€“$120 | ~$40โ€“$60 | **$40โ€“$60** | | **Azure CDN** | $0.087 | ~$87 | ~$43.5 | **$43.5** | > ๐Ÿ’ก These are just for **1 TB/month**. At scale (10 TB+), savings scale to **$400โ€“$1,000+ per month**. --- ### โœ… Summary - `tiny-json` helps reduce **egress traffic costs** by compressing repetitive JSON structures. - Also minimizes **CDN cache space** for better hit rates and faster delivery. - Ideal for high-traffic systems: **mobile APIs, GraphQL, SaaS, IoT**, and **data-intensive dashboards**. ## ๐Ÿ“ฆ Install ```bash npm i @krishnadaspc/tiny-json ``` --- ## ๐Ÿ”ง Usage ```js import { compress, decompress, analyze } from '@krishnadaspc/tiny-json'; const originalData = { currentPage: 1, totalPages: 5, pageSize: 20, totalCount: 100, records: Array.from({ length: 436 }, (_, i) => ({ userId: i, userName: "john_doe", userEmail: "john@example.com", userRole: "admin", userStatus: "active", address: { city: "New York", zip: "10001", country: "USA" }, contact: { phone: "+1-555-1234567", alternate: "+1-555-0000000" } })) } // ๐Ÿ” Compress the JSON const compressed = compress(originalData); console.log("Compressed:", compressed); // ๐Ÿ”“ Decompress it back const restored = decompress(compressed); console.log("Restored:", restored); // ๐Ÿ“Š Show compression stats const stats = analyze(originalData); console.log( stats); ``` --- ## ๐Ÿ“Š Output Example ```js { originalSize: '100.02 KB', compressedSize: '54.50 KB', reductionPercent: 45.51 } ``` --- ## ๐Ÿงช Benchmarking in Browser Use `benchmark.js` to test performance in real API scenarios: ```js // benchmark.js benchmark('https://dummyjson.com/products?limit=100'); ``` --- ## ๐Ÿ†š Compression Comparison | Method | Typical Size Reduction | Notes | |---------------------|-------------------------|----------------------------------------| | `tiny-json` | 40โ€“50% | Best for repetitive keys/values | | GZIP (HTTP) | 50โ€“75% | Standard byte-level compression | | `tiny-json` + GZIP | 65โ€“85% | โœ… Best results when combined | Example: A 100KB JSON payload may reduce to: | Stage | Final Size | |-----------------------|-------------| | Raw JSON | 100 KB | | With `tiny-json` | 54โ€“60 KB | | With GZIP only | 30โ€“45 KB | | With both | 15โ€“25 KB | --- ## ๐Ÿ›  API ### `compress(data: object): object` Compresses JSON by renaming keys and repeated values. ### `decompress(compressed: object): object` Reverses the transformation and restores original data. ### `analyze(data: object, unit?: 'bytes' | 'kb' | 'mb'): object` Returns compression stats.