{"id":15022036,"url":"https://github.com/mitschabaude/fast-base64","last_synced_at":"2025-04-12T06:05:10.555Z","repository":{"id":57232886,"uuid":"388087194","full_name":"mitschabaude/fast-base64","owner":"mitschabaude","description":"Fastest base64 on the web, with Wasm + SIMD","archived":false,"fork":false,"pushed_at":"2022-01-04T09:16:56.000Z","size":308,"stargazers_count":54,"open_issues_count":4,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T06:04:01.918Z","etag":null,"topics":["base64","deno","simd","speed","webassembly"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/mitschabaude.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-21T10:57:16.000Z","updated_at":"2025-04-10T19:42:43.000Z","dependencies_parsed_at":"2022-08-31T14:11:14.885Z","dependency_job_id":null,"html_url":"https://github.com/mitschabaude/fast-base64","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitschabaude%2Ffast-base64","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitschabaude%2Ffast-base64/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitschabaude%2Ffast-base64/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitschabaude%2Ffast-base64/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitschabaude","download_url":"https://codeload.github.com/mitschabaude/fast-base64/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525144,"owners_count":21118618,"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":["base64","deno","simd","speed","webassembly"],"created_at":"2024-09-24T19:57:21.872Z","updated_at":"2025-04-12T06:05:10.494Z","avatar_url":"https://github.com/mitschabaude.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fast-base64\n\nBase64 encoding/decoding optimized for speed. Converts base64 to and from `Uint8Array`.\n\n- Hand-written in WebAssembly text format for small size.\n- Uses SIMD instructions if the Browser supports it.\n- Compatible with `node` and `deno`.\n- ~20x faster than the fastest JS implementation on the first 100kB of input\n- ~2-3x faster than the fastest JS on the first 100MB\n- The fastest existing JS implementation is the one shipped in this package\n- The smallest existing implementation is also shipped in this package, because why not 😁\n\n```sh\nnpm install fast-base64\n```\n\n```js\nimport {toBytes, toBase64} from 'fast-base64';\n\nlet bytes = await toBytes('SGVsbG8sIHdvcmxkIQ==');\nlet base64 = await toBase64(bytes);\n```\n\nOr, using `deno`:\n\n```js\nimport {toBytes, toBase64} from 'https://deno.land/x/fast_base64/mod.ts';\n```\n\n## Alternative imports\n\nWe support four versions of the library that have different speed and size trade-offs.\n\n- `fast-base64`: The default is the fastest version, 1.9kB minzipped, **async** API\n- `fast-base64/small`: Version without SIMD, 1.0kB minzipped, **async** API, no `node` support, 2-3x slower\n- `fast-base64/js`: Fastest pure JS version, 600 bytes minzipped, **sync** API, 2-30x slower\n- `fast-base64/nano`: Smallest possible version, 147 bytes, **sync** API, no `node` support, 3-100x slower\n\nThe APIs are all equivalent, except that the latter two are synchronous. Example for using the fast JS version:\n\n```js\nimport {toBytes, toBase64} from 'fast-base64/js';\n\nlet bytes = toBytes('SGVsbG8sIHdvcmxkIQ=='); // no `await`!\nlet base64 = toBase64(bytes);\n```\n\nDISCLAIMER: You probably don't _need_ speed-optimized base64. `fast-base64/nano`, the slowest of all versions that I tried, could even be the best choice for typical applications, because the speed difference will simply not be noticable if payloads are not huge. For example, on my laptop, 10kB of base64 decode in 0.06ms with `fast-base64` and in 5ms with `fast-base64/nano`.\n\n## Base64 URL\n\nTo support base64url, we offer two tiny helper functions.\n\n```js\nimport {toUrl, fromUrl} from 'fast-base64/url';\n\nlet base64url = toUrl('/+A='); // \"_-A\"\nlet base64 = fromUrl(base64url); // \"/+A=\"\n```\n\nThe added runtime overhead when combining these with the fastest de-/encoder is about 2-4x, which should be fine for almost all circumstances.\n\n## Wouldn't this be _even faster_ with threading?\n\nSadly, no. This repository includes threaded variants of both the Wasm and pure JS encoding/decoding, where I distribute the workload between multiple Web Workers and join their results once all are complete. You can check out the code in [./wasm-threads.js](https://github.com/mitschabaude/fast-base64/blob/main/wasm-threads.js) and [./js-threads.js](https://github.com/mitschabaude/fast-base64/blob/main/js-threads.js).\n\nThese turn out to be not faster than the single-threaded versions, irrespective of the number of workers, except for very large payloads (\u003e 1MB) in the pure JS version (where 3-4 workers can provide a speed-up of 1.5-2x). Especially the Wasm version with threads is clearly slower. It also comes with a larger bundle size and worse browser support.\n\nAs far as I can tell, the added overhead of slicing up the input, messaging to the workers and back, and recombining the results is bigger than the gains in performing the actual calculation.\n\n## Curious about Base64?\n\nIn making this package, I tried many different approaches for base64 encoding, including using the native `atob()` and `btoa()` functions and native data URL functionality. You can find 4 alternative encoding and 3 decoding methods here: [./alternative.js](https://github.com/mitschabaude/fast-base64/blob/main/alternative.js)\n\nTurns out that one of the fastest bytes to base64 implementations in JS uses the FileReader API 😮\n\n```js\nasync function toBase64DataUri(bytes) {\n  let reader = new FileReader();\n  let promise = new Promise(resolve =\u003e {\n    reader.onload = () =\u003e resolve(reader.result);\n  });\n  let blob = new Blob([bytes.buffer], {type: 'application/octet-stream'});\n  reader.readAsDataURL(blob);\n  return (await promise).replace('data:application/octet-stream;base64,', '');\n}\n```\n\nOh, and maybe you can decipher `fast-base64/nano`?\n\n```js\nlet y = s =\u003e Uint8Array.from(atob(s), c =\u003e c.charCodeAt(0)),\n  a = b =\u003e btoa([...b].map(x =\u003e String.fromCharCode(x)).join(''));\nexport {y as toBytes, a as toBase64};\n```\n\nIf you want to compare and possibly tune performance by yourself, try running `yarn build` and `npx chrodemon test-base64.js` in the cloned repo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitschabaude%2Ffast-base64","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitschabaude%2Ffast-base64","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitschabaude%2Ffast-base64/lists"}