@pixagram/turbobase64
v0.0.2
Published
Ultra-optimized JavaScript base64. SIMD optimization.
Downloads
54
Maintainers
Readme
@pixagram/turbobase64
A high-performance JavaScript implementation of Base64 encoding and decoding, inspired by the SIMD-accelerated C library, TurboBase64. This module is designed for speed, processing data in large chunks to optimize performance.
Features
High-Speed Encoding & Decoding: Optimized for performance by processing data in large blocks.
Pure JavaScript: No native dependencies, runs in Node.js and modern browsers.
Uint8Array Support: Works directly with Uint8Array for efficient binary data handling.
Inspired by TurboBase64: Aims to bring the performance philosophy of the original C library to the JavaScript ecosystem.
Installation
Install the package using npm:
npm install @pixagram/turbobase64
Usage
Here's a quick example of how to use the TurboBase64 class to encode and decode data.
import TurboBase64 from '@pixagram/turbobase64';
// Or if you are using CommonJS:
// const TurboBase64 = require('@pixagram/turbobase64');
const turboB64 = new TurboBase64();
// --- Encoding ---
const originalString = "Fast and efficient Base64 encoding!";
const textEncoder = new TextEncoder();
const originalData = textEncoder.encode(originalString);
const encodedString = turboB64.encode(originalData);
console.log("Encoded:", encodedString);
// Output: "RmFzdCBhbmQgZWZmaWNpZW50IEJhc2U2NCBlbmNvZGluZyE="
// --- Decoding ---
const decodedData = turboB64.decode(encodedString);
const textDecoder = new TextDecoder();
const decodedString = textDecoder.decode(decodedData);
console.log("Decoded:", decodedString);
// Output: "Fast and efficient Base64 encoding!"
console.log("Match:", originalString === decodedString);
// Output: true
How It Works
This library's performance comes from its strategy of processing data in large, fixed-size chunks (12 bytes for encoding, 16 bytes for decoding). While the original C library relies on SSE/AVX intrinsics for maximum speed, this JavaScript version uses highly optimized scalar operations on these chunks. This approach avoids the overhead of processing byte-by-byte, resulting in a significant speed advantage for large data sets compared to more traditional Base64 implementations in JavaScript.
A simd.js polyfill is included for compatibility, but the core transformation logic relies on proven scalar bitwise operations for correctness and reliability across all JavaScript environments.
API
new TurboBase64()
Creates a new TurboBase64 instance. The constructor initializes the necessary lookup tables for encoding and decoding.
turboB64.encode(input)
input: Uint8Array - The binary data to encode.
Returns: string - The Base64 encoded string.
turboB64.decode(input)
input: string - The Base64 encoded string.
Returns: Uint8Array - The decoded binary data.
License
The original C source code for TurboBase64 is licensed under the GPL v3 License. This JavaScript adaptation is also released under the GPL v3 License.
