New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fast-base64

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-base64

Fastest possible base64 encoding/decoding using WebAssembly

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-99.61%
Maintainers
1
Weekly downloads
 
Created
Source

fast-base64

Base64 encoding/decoding optimized for speed. Converts base64 to and from Uint8Array.

  • Hand-written in WebAssembly text format for small size.
  • Uses SIMD instructions if the Browser supports it.
  • Compatible with node and deno.
  • ~20x faster than the fastest JS implementation on the first 100kB of input
  • ~2-3x faster than the fastest JS on the first 100MB
  • The fastest existing JS implementation is the one shipped in this package
npm install fast-base64
import {toBytes, toBase64} from 'fast-base64';

let bytes = await toBytes('SGVsbG8sIHdvcmxkIQ==');
let base64 = await toBase64(bytes);

Alternative exports

We support three versions of the library that have different speed and size trade-offs

  • fast-base64: The default is the fastest version, 1.9kB minzipped, async API
  • fast-base64/small: Version without SIMD, 1.0kB minzipped, async API, no node support, 2-3x slower
  • fast-base64/js: Fastest pure JS version, 600 bytes minzipped, sync API, 2-30x slower

Example for using the pure JS version:

import {toBytes, toBase64} from 'fast-base64/js';

let bytes = toBytes('SGVsbG8sIHdvcmxkIQ=='); // no `await`!
let base64 = toBase64(bytes);

Base64 URL

To support base64url we offer two tiny, fast helper functions (the runtime overhead compared with base64 transcoding itself is negligible):

import {toUrl, fromUrl} from 'fast-base64/url';

let base64url = toUrl('/+A='); // "_-A"
let base64 = fromUrl(base64url); // "/+A="

// you could make our own helper functions for base64url
const toBase64Url = async bytes => toUrl(await toBase64(bytes));
const toBytesUrl = async base64url => await toBytes(fromUrl(base64url));

Curious about Base64?

In making this package, I tried many different approaches for base64 encoding, including using the native atob() and btoa() functions and native dataURI functionality. You can find 4 alternative encoding and 3 decoding methods here: https://github.com/mitschabaude/fast-base64/blob/main/base64-alternative.js

Turns out that one of the fastest bytes to base64 implementations in JS uses the good old FileReader API!

async function toBase64DataUri(bytes) {
  let reader = new FileReader();
  let promise = new Promise(resolve => {
    reader.onload = () => resolve(reader.result);
  });
  let blob = new Blob([bytes.buffer], {type: 'application/octet-stream'});
  reader.readAsDataURL(blob);
  return (await promise).replace('data:application/octet-stream;base64,', '');
}

Keywords

FAQs

Package last updated on 27 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc