{"id":25238631,"url":"https://github.com/elysiajs/json-accelerator","last_synced_at":"2025-10-26T14:30:37.307Z","repository":{"id":275603890,"uuid":"926584146","full_name":"elysiajs/json-accelerator","owner":"elysiajs","description":"Accelerate JSON stringification by providing OpenAPI/TypeBox shape","archived":false,"fork":false,"pushed_at":"2025-04-30T03:05:21.000Z","size":131,"stargazers_count":518,"open_issues_count":11,"forks_count":13,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-10-16T00:35:30.971Z","etag":null,"topics":["accelerator","json","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/json-accelerator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elysiajs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yaml","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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"SaltyAom"}},"created_at":"2025-02-03T14:15:20.000Z","updated_at":"2025-08-30T13:50:43.000Z","dependencies_parsed_at":"2025-02-03T15:47:54.985Z","dependency_job_id":"f1b49952-a51e-4ce3-9851-090636ffd127","html_url":"https://github.com/elysiajs/json-accelerator","commit_stats":null,"previous_names":["elysiajs/json-accelerator"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/elysiajs/json-accelerator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elysiajs%2Fjson-accelerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elysiajs%2Fjson-accelerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elysiajs%2Fjson-accelerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elysiajs%2Fjson-accelerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elysiajs","download_url":"https://codeload.github.com/elysiajs/json-accelerator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elysiajs%2Fjson-accelerator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280931465,"owners_count":26415732,"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","status":"online","status_checked_at":"2025-10-25T02:00:06.499Z","response_time":81,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["accelerator","json","typescript"],"created_at":"2025-02-11T17:52:51.491Z","updated_at":"2025-10-26T14:30:37.299Z","avatar_url":"https://github.com/elysiajs.png","language":"TypeScript","funding_links":["https://github.com/sponsors/SaltyAom"],"categories":["TypeScript"],"sub_categories":[],"readme":"# JSON Accelerator\n\nAccelerate JSON stringification by providing OpenAPI/TypeBox model.\n\nBy providing model ahead of time, the library will generate a function that will serialize the object into a JSON string.\n\n```\n$ npx tsx benchmarks/medium-manual.ts\n\nclk: ~3.02 GHz\ncpu: Apple M1 Max\nruntime: node 22.6.0 (arm64-darwin)\n\nsummary\n  JSON Accelerator\n   2.12x faster than JSON Stingify\n   2.66x faster than Fast Json Stringify\n```\n\n## Installation\n\n```bash\n# Using either one of the package manager\nnpm install json-accelerator\nyarn add json-accelerator\npnpm add json-accelerator\nbun add json-accelerator\n```\n\n## Usage\n\nIt is designed to be used with [TypeBox](https://github.com/sinclairzx81/typebox) but an OpenAPI schema should also work.\n\n```typescript\nimport { Type as t } from '@sinclair/typebox'\nimport { createAccelerator } from 'json-accelerator'\n\nconst shape = t.Object({\n\tname: t.String(),\n\tid: t.Number()\n})\n\nconst value = {\n\tid: 0,\n\tname: 'saltyaom'\n} satisfies typeof shape.static\n\nconst encode = createAccelerator(shape)\n\nconsole.log(encode(value)) // {\"id\":0,\"name\":\"saltyaom\"}\n```\n\n## Caveat\n\nThis library **WILL NOT** check for the type validity of the schema, it is expected that the schema is **always** correct.\n\nThis can be achieved by checking the input validity with TypeBox before passing it to the accelerator.\n\n```typescript\nimport { Type as t } from '@sinclair/typebox'\nimport { TypeCompiler } from '@sinclair/typebox/compiler'\nimport { createAccelerator } from 'json-accelerator'\n\nconst shape = t.Object({\n\tname: t.String(),\n\tid: t.Number()\n})\n\nconst value = {\n\tid: 0,\n\tname: 'saltyaom'\n}\n\nconst guard = TypeCompiler.Compile(shape)\nconst encode = createAccelerator(shape)\n\nif (guard.Check(value)) encode(value)\n```\n\nIf the shape is incorrect, the output will try to corece the value into an expected model but if failed the error will be thrown.\n\n## Options\n\nThis section is used to configure the behavior of the encoder.\n\n`Options` can be passed as a second argument to `createAccelerator`.\n\n```ts\ncreateAccelerator(shape, {\n\tunsafe: 'throw'\n})\n```\n\n## Unsafe\n\nIf unsafe character is found, how should the encoder handle it?\n\nThis value only applied to string field.\n\n- `'auto'`: Sanitize the string and continue encoding\n- `'manual'`: Ignore the unsafe character, this implied that end user should specify fields that should be sanitized manually\n- `'throw'`: Throw an error\n\nThe default behavior is `auto`.\n\n### format sanitize\n\nSince this library is designed for a controlled environment (eg. Restful API), most fields are controlled by end user which doesn't include unsafe characters for JSON encoding.\n\nWe can improve performance by specifying a `sanitize: 'manual'` and provide a field that should be sanitized manually.\n\nWe can add `sanitize: true` to a schema which is an uncontrolled field submit by user that might contains unsafe characters.\n\nWhen a field is marked as `sanitize`, the encoder will sanitize the string and continue encoding regardless of the `unsafe` configuration.\n\n```ts\nimport { Type as t } from '@sinclair/typebox'\nimport { createAccelerator } from 'json-accelerator'\n\nconst shape = t.Object({\n\tname: t.String(),\n\tid: t.Number(),\n\tunknown: t.String({ sanitize: true })\n})\n\nconst value = {\n\tid: 0,\n\tname: 'saltyaom',\n\tunknown: `hello\\nworld`\n} satisfies typeof shape.static\n\nconst encode = createAccelerator(shape, {\n\tsanitize: 'manual'\n})\n\nconsole.log(encode(value)) // {\"id\":0,\"name\":\"saltyaom\",\"unknown\":\"hello\\\\nworld\"}\n```\n\nThis allows us to speed up a hardcode\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felysiajs%2Fjson-accelerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felysiajs%2Fjson-accelerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felysiajs%2Fjson-accelerator/lists"}