All Projects → lukechilds → Base64 Async

lukechilds / Base64 Async

Licence: mit
Non-blocking chunked Base64 encoding

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Base64 Async

Ws Machine
WS-Machine is a websocket finite state machine for client websocket connections (Go)
Stars: ✭ 110 (+12.24%)
Mutual labels:  async, asynchronous, non-blocking
Parallel Ssh
Asynchronous parallel SSH client library.
Stars: ✭ 864 (+781.63%)
Mutual labels:  async, asynchronous, non-blocking
Request.swift
A tiny HTTP client written in swift. URLSession alternative
Stars: ✭ 14 (-85.71%)
Mutual labels:  async, asynchronous
Iguazu Rest
✨ Iguazu REST is a plugin for the Iguazu ecosystem that allows for pre-built async calls for REST with smart caching.
Stars: ✭ 21 (-78.57%)
Mutual labels:  async, asynchronous
Futures Batch
An adapter for futures, which chunks up elements and flushes them after a timeout, or when the buffer is full. (Formerly known as tokio-batch.)
Stars: ✭ 37 (-62.24%)
Mutual labels:  async, chunk
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-76.53%)
Mutual labels:  async, asynchronous
Chili
Chili: HTTP Served Hot
Stars: ✭ 7 (-92.86%)
Mutual labels:  async, asynchronous
Handle Path Oz
Android Library to handle multiple Uri's(paths) received through Intents.
Stars: ✭ 36 (-63.27%)
Mutual labels:  async, asynchronous
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+604.08%)
Mutual labels:  async, asynchronous
Ea Async
EA Async implements async-await methods in the JVM.
Stars: ✭ 1,085 (+1007.14%)
Mutual labels:  async, asynchronous
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-50%)
Mutual labels:  async, asynchronous
Ktor
Framework for quickly creating connected applications in Kotlin with minimal effort
Stars: ✭ 9,190 (+9277.55%)
Mutual labels:  async, asynchronous
Blockly Gamepad
A Blockly extension designed to develop games (made with love ❤)
Stars: ✭ 18 (-81.63%)
Mutual labels:  async, asynchronous
Recoil
Asynchronous coroutines for PHP 7.
Stars: ✭ 765 (+680.61%)
Mutual labels:  async, asynchronous
Vertx Sql Client
High performance reactive SQL Client written in Java
Stars: ✭ 690 (+604.08%)
Mutual labels:  async, non-blocking
Csp
Communicating Sequential Processes in JavaScript
Stars: ✭ 33 (-66.33%)
Mutual labels:  async, asynchronous
Framework
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.
Stars: ✭ 1,125 (+1047.96%)
Mutual labels:  async, asynchronous
Reservoir
Android library to easily serialize and cache your objects to disk using key/value pairs.
Stars: ✭ 674 (+587.76%)
Mutual labels:  async, asynchronous
G3log
G3log is an asynchronous, "crash safe", logger that is easy to use with default logging sinks or you can add your own. G3log is made with plain C++14 (C++11 support up to release 1.3.2) with no external libraries (except gtest used for unit tests). G3log is made to be cross-platform, currently running on OSX, Windows and several Linux distros. See Readme below for details of usage.
Stars: ✭ 677 (+590.82%)
Mutual labels:  async, asynchronous
Google Books Android Viewer
Android library to bridge between RecyclerView and sources like web page or database. Includes demonstrator (Google Books viewer)
Stars: ✭ 37 (-62.24%)
Mutual labels:  async, chunk

base64-async

Non-blocking chunked Base64 encoding

Build Status Coverage Status npm

Process large Base64 documents without blocking the event loop.

Configurable chunk size option to optimise for your use case.

Note:

Base64 in Node.js is already crazy fast. Breaking the work up into chunks and adding async logic adds overhead. If you aren't dealing with large files it will probably be more efficient to just block the event loop for the small amount of time it takes Node.js to process Base64 synchronously.

Install

npm install --save base64-async

Usage

const b64 = require('base64-async');
const fs = require('fs');
const buffer = fs.readFileSync('somehugefile.jpg');

b64.encode(buffer).then(b64String => console.log(b64String));
// aGkgbXVt...

b64.decode(b64String).then(buffer => console.log(buffer));
// <Buffer 68 69 20 6d 75 6d ... >

// or, for the cool kids
const b64String = await b64.encode(buffer);
const buffer = await b64.decode(b64String);

// which is equivalent to this
const b64String = await b64(buffer);
const buffer = await b64(b64String);
// If no method is specified, buffers are encoded, strings are decoded

Example

$ npm run example

Registering 4 asynchronous jobs...
Encoding 100 MB with default Node.js Buffer API...
Base64 encode complete
Hi, I'm an asynchronous job, and I'm late by 231ms
Hi, I'm an asynchronous job, and I'm late by 238ms
Hi, I'm an asynchronous job, and I'm late by 239ms
Hi, I'm an asynchronous job, and I'm late by 245ms

Registering 4 asynchronous jobs...
Encoding 100 MB with base64-async in chunks of 250 kB...
Hi, I'm an asynchronous job, and I'm on time
Hi, I'm an asynchronous job, and I'm on time
Hi, I'm an asynchronous job, and I'm on time
Hi, I'm an asynchronous job, and I'm on time
Base64 encode complete

(example source code)

Notice how none of the async jobs can start until the Buffer API has finished encoding and stops blocking the event loop? With base64-async the async jobs can execute in-between each chunk of data.

Performance

$ npm run bench

Benchmark completed with a chunk size of 250 kB
┌────────┬──────────────┬──────────────┬──────────────┬──────────────┐
│ Bytes  │ Encode Sync  │ Decode Sync  │ Encode Async │ Decode Async │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 10 kB  │ 0.097225ms   │ 0.383031ms   │ 1.276201ms   │ 0.537687ms   │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 100 kB │ 0.198161ms   │ 0.271577ms   │ 0.99799ms    │ 0.356765ms   │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 1 MB   │ 1.924415ms   │ 2.038406ms   │ 2.679117ms   │ 2.544993ms   │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 10 MB  │ 15.749204ms  │ 16.280246ms  │ 33.666111ms  │ 29.918725ms  │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 100 MB │ 165.189455ms │ 195.298199ms │ 246.359068ms │ 280.792751ms │
└────────┴──────────────┴──────────────┴──────────────┴──────────────┘

As you can see, the total processing time is longer with base64-async (as we spend some time paused waiting for the event loop). However, if you have an idea of the size of the data you'll be working with, you can play around with the chunk size to get better performance.

The included benchmarking tool accepts arguments to help you test this:

$ npm run bench -- --chunkSize=1000000 --bytesToBenchmark=50000000,100000000

Benchmark completed with a chunk size of 1 MB
┌────────┬──────────────┬──────────────┬──────────────┬──────────────┐
│ Bytes  │ Encode Sync  │ Decode Sync  │ Encode Async │ Decode Async │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 50 MB  │ 79.675533ms  │ 87.251079ms  │ 92.400367ms  │ 137.468082ms │
├────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ 100 MB │ 203.423705ms │ 173.567974ms │ 186.181857ms │ 264.123311ms │
└────────┴──────────────┴──────────────┴──────────────┴──────────────┘

API

b64(input, [options])

Returns a Promise that resolves to the Base64 encoded/decoded input.

input

Type: string, buffer

A Base64 string to decode, or a Buffer to encode.

options

Type: object

options.chunkSize

Type: number
Default: 250000

Size of the chunk of data to work on before deferring execution to the next iteration of the event loop.

If encoding, the number is interpreted as number of bytes. If decoding, the number is interpreted as number of characters.

b64.encode(input, [options])

Returns a Promise that resolves to a Base64 encoded string.

input

Type: buffer

A Buffer to encode.

b64.decode(input, [options])

Returns a Promise that resolves to a decoded Buffer.

input

Type: string

A Base64 string to decode.

License

MIT © Luke Childs

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].