base64-js vs js-base64 vs base-64 vs btoa
Choosing Base64 for byte arrays, binary strings or Unicode text
base64-jsjs-base64base-64btoa

Choosing Base64 for byte arrays, binary strings or Unicode text

The main choice is the data representation at the boundary. These packages do not all accept the same kind of string or return the same kind of decoded value.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
base64-js240,400,7968799.62 kB56 years agoMIT
js-base6430,291,4214,36039.8 kB08 days agoBSD-3-Clause
base-6410,263,944520-126 years agoMIT
btoa7,518,568---9 years ago(MIT OR Apache-2.0)

Text and bytes are different inputs

Base-64 describes input strings whose character values represent bytes. A general JavaScript Unicode string is not automatically the same representation.

Base64-js works directly with byte arrays. Its toByteArray and fromByteArray routes make the binary boundary explicit instead of using a string as a carrier for bytes.

Similar names can hide different decoding

Js-base64 documents decode for UTF-8 text and toUint8Array for bytes. Its atob-style result is a binary string, so it is not interchangeable with decode for arbitrary text.

The btoa package is a Node Buffer-based port. Its own README notes Unicode differences, so its sample output should not be treated as a general text-encoding contract. A same-named browser function does not prove every unsupported-character behaviour is identical.

Choose the boundary before the helper

Decide whether the source is text with a chosen encoding or an existing byte sequence, then choose the corresponding API. Map encoded-string format and decoded-result type during migration. A successful ASCII example alone does not establish that non-ASCII text or arbitrary binary data will survive the same replacement.

How to Choose: base64-js vs js-base64 vs base-64 vs btoa

  • base64-js:

    Choose byte arrays and the toByteArray/fromByteArray/byteLength interfaces for binary data.

  • js-base64:

    Choose the documented Unicode-text or byte-array helpers according to the input/output required, keeping decode distinct from atob.

  • base-64:

    Choose its atob/btoa-style byte-string model when characters represent byte values; prepare Unicode text with an explicit encoding first.

  • btoa:

    Choose only when its Node Buffer-based browser-function compatibility route is the intended integration; do not assume arbitrary Unicode text round-trips through it.

README for base64-js

base64-js

base64-js does basic base64 encoding/decoding in pure JS.

build status

Many browsers already have base64 encoding/decoding functionality, but it is for text data, not all-purpose binary data.

Sometimes encoding/decoding binary data in the browser is useful, and that is what this module does.

install

With npm do:

npm install base64-js and var base64js = require('base64-js')

For use in web browsers do:

<script src="base64js.min.js"></script>

Get supported base64-js with the Tidelift Subscription

methods

base64js has three exposed functions, byteLength, toByteArray and fromByteArray, which both take a single argument.

  • byteLength - Takes a base64 string and returns length of byte array
  • toByteArray - Takes a base64 string and returns a byte array
  • fromByteArray - Takes a byte array and returns a base64 string

license

MIT