{"id":18465664,"url":"https://github.com/oct16/brick","last_synced_at":"2025-04-08T08:31:58.229Z","repository":{"id":90662111,"uuid":"313066346","full_name":"oct16/brick","owner":"oct16","description":"An algorithm for compress JSON, up to 15% better than Gzip","archived":false,"fork":false,"pushed_at":"2020-11-23T12:22:53.000Z","size":620,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-23T19:24:33.927Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oct16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-11-15T15:52:54.000Z","updated_at":"2022-01-08T08:42:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"0c8984b3-a750-40db-b421-320c98cd1ba5","html_url":"https://github.com/oct16/brick","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"47d7dbe86dac4afb002d846be855ce0e9de4220b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oct16%2Fbrick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oct16%2Fbrick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oct16%2Fbrick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oct16%2Fbrick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oct16","download_url":"https://codeload.github.com/oct16/brick/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247804597,"owners_count":20999018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T09:13:51.433Z","updated_at":"2025-04-08T08:31:57.709Z","avatar_url":"https://github.com/oct16.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Brick.Json\n[![Coverage Status](https://coveralls.io/repos/github/oct16/brick/badge.svg)](https://coveralls.io/github/oct16/brick)\n#### An algorithm for compress JSON, up to 15% better than Gzip\n\n### Installation\n```bash\n$ npm install brick.json\n# or\n$ yarn add brick.json\n```\n### Usage\n#### Basic\n```ts\nimport { compress, decompress } from 'brick.json'\n\nconst data = [{a: 1, b: 2}, [1, 2, 3]]\n\nconst brickData = compress(data)\n\nconst res = decompress(brickData) // res is deep equal data\n\n```\n\n#### With Gzip\n```ts\nimport { compressWithGzip, decompressWithGzip } from 'brick.json/gzip/cjs' // or esm\n\nconst data = [{a: 1, b: 2}, [1, 2, 3]]\n\nconst str = compressWithGzip(data) // type is string\n\nconst res = decompressWithGzip(str) // res is deep equal data\n\n```\n### Background\n\nSometimes our browser will upload some data to the server, but as the sender, the browser does not know which compression algorithms are supported by the service, so by default the uploaded data is not compressed, which is why this solution exists.\nThis lib used a cjson-like algorithms approach to de-weight duplicate keys to reduce the json size. As the compressed data resembles a lot of bricks 🧱🧱🧱, so it is named Brick.Json\n### How it's work\n\nA raw JSON object like below\n\n```json\n[\n    {\n        \"a\": \"aa\",\n        \"b\": \"bb\",\n        \"c\": \"cc\"\n    },\n    {\n        \"a\": \"2A\",\n        \"b\": {\n            \"b\": \"B\",\n            \"c\": \"C\",\n            \"d\": [\n                true,\n                null,\n                false\n            ],\n            \"a\": \"A\"\n        }\n    }\n]\n```\nAfter **Brick.Json** compress\n\n```js\n[\n    [[1,\"c\"],[\"a\",\"b\"],[0,\"d\"]], // keys array -\u003e [ abc, ab, abcd ]\n    [\"$\",[0,\"aa\",\"bb\",\"cc\"],[1,\"2A\",[2,\"A\",\"B\",\"C\",[\"$\",true,null,false]]]] // values array\n//    ↑                              ↑\n//  start with $, is an array      start with number, is an object and will find the keys\n]\n```\n\n#### Explanation\n\nThe first array holds the keys and the second array holds values\nIn the case of an object, the first value is a index of the key by which you can find the object's key, followed by the values\nIn the case of an array, the values of the array are ordered by an identifying a tag, followed by the values\n \n### How it's compression rate and performance\n\n##### Platform: MacBook Pro 2018 6C16G\n\n```\n┌───────────────────────┬────────────────────┬───────────────────┬──────────────────────┬──────────────────────┬─────────────────────┬───────────────────┐\n│ Algorithm \\ Data      │ Data1 (Object)     │ Data2 (Object)    │ Data3 (List1)        │ Data4 (List2)        │ Data5 (JueJin)      │ Data6 (Random)    │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ Raw                   │ 5842 100%          │ 3302 100%         │ 481714 100%          │ 972195 100%          │ 40949 100%          │ 2401 100%         │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ BrickJson             │ 5628 96.34% 0.2ms  │ 2455 74.35% 0.3ms │ 366456 76.07% 6.6ms  │ 738755 75.99% 11.4ms │ 19481 47.57% 1.1ms  │ 2350 97.88% 2.3ms │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ JSONH                 │ Not Support        │ Not Support       │ 469245 97.41% 1.2ms  │ 946826 97.39% 2.3ms  │ Not Support         │ Error             │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ JSONH With Gzip       │ Not Support        │ Not Support       │ 34161 7.09% 56.1ms   │ 68456 7.04% 33.6ms   │ Not Support         │ Error             │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ JSONH With Brotli     │ Not Support        │ Not Support       │ 18225 3.78% 1014.2ms │ 19009 1.96% 864.9ms  │ Not Support         │ Error             │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ Brotli                │ 1048 17.94% 18.9ms │ 825 24.98% 14.2ms │ 18392 3.82% 851.2ms  │ 19097 1.96% 882.5ms  │ 7436 18.16% 152.1ms │ 1023 42.61% 7.4ms │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ BrickJson With Brotli │ 1092 18.69% 17.2ms │ 837 25.35% 11.1ms │ 18140 3.77% 671.1ms  │ 18805 1.93% 686.2ms  │ 7119 17.39% 85.4ms  │ 1109 46.19% 7.6ms │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ Gzip                  │ 1245 21.31% 0.7ms  │ 1124 34.04% 1.3ms │ 35127 7.29% 41.0ms   │ 69507 7.15% 49.1ms   │ 11486 28.05% 8.9ms  │ 1169 48.69% 0.4ms │\n├───────────────────────┼────────────────────┼───────────────────┼──────────────────────┼──────────────────────┼─────────────────────┼───────────────────┤\n│ BrickJson With Gzip   │ 1279 21.89% 10.0ms │ 1103 33.40% 1.2ms │ 31518 6.54% 24.9ms   │ 62365 6.41% 43.3ms   │ 10467 25.56% 3.3ms  │ 1287 53.60% 4.5ms │\n└───────────────────────┴────────────────────┴───────────────────┴──────────────────────┴──────────────────────┴─────────────────────┴───────────────────┘\nBrkckJson compress: data1: 124 line x 15,105 ops/sec ±0.58% (85 runs sampled)\nBrkckJson compress: data3: 10391 line x 250 ops/sec ±0.47% (82 runs sampled)\ngzip      compress: data3: 10391 line x 56.43 ops/sec ±0.19% (70 runs sampled)\nbrotli    compress: data3: 10391 line x 1.17 ops/sec ±0.29% (7 runs sampled)\ncalcJSONH compress: data3: 10391 line x 6.69 ops/sec ±0.44% (21 runs sampled)\nBrkckJson decompress: data1 x 22,616 ops/sec ±0.27% (92 runs sampled)\nBrkckJson decompress: data3 x 645 ops/sec ±0.35% (90 runs sampled)\nBrkckJson compressWithGzip: data1 x 2,113 ops/sec ±0.71% (91 runs sampled)\nBrkckJson compressWithGzip: data3 x 59.69 ops/sec ±0.33% (60 runs sampled)\nBrkckJson decompressWithGzip: data1 x 3,938 ops/sec ±0.49% (89 runs sampled)\nBrkckJson decompressWithGzip: data3 x 54.18 ops/sec ±0.55% (68 runs sampled)\n```\n\n### Conclusion\n\n1. Brotli has a high compression rate, but the performance is not very good, the compression speed is about 10 times slower than Gzip!\n\n2. With Brotli and Gzip together, performance is similar, but compression ratios can be improved by around 10%!\n\n3. Brick.Json is more compatible and more robust than JSONH\n\n### recommendation\n\n1. Transfer data from server to client, choose [Google Brotli](https://github.com/google/brotli)\n2. Transfer data from client to server, choose Gzip or [Brick.Json](https://github.com/oct16/brick)\n\n### Alternatives\n\n- [jsonh](https://github.com/WebReflection/JSONH)\n- [jsonc](https://github.com/tcorral/JSONC)\n### Referrer\n\n[Web Resource Optimization](http://web-resource-optimization.blogspot.com/2011/06/json-compression-algorithms.html)\n\n### License\n\nThe code is licensed under the copyleft GPL-3.0. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foct16%2Fbrick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foct16%2Fbrick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foct16%2Fbrick/lists"}