What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

If you already have a Uint8Array, convert it directly with bytes.toBase64() or bytes.toHex(). That avoids first turning each byte into a JavaScript string character just to pass the string to btoa(). For Unicode text, encode the text as bytes first—typically with TextEncoder—then convert those bytes.

Convert a Uint8Array directly to Base64 or hex

The typed-array methods return strings representing the array’s bytes:

const bytes = new Uint8Array([72, 101, 108, 108, 111]);

const base64 = bytes.toBase64(); // "SGVsbG8="
const hex = bytes.toHex();      // "48656c6c6f"

For the bytes representing “Hello World,” the TC39 proposal’s examples produce SGVsbG8gV29ybGQ= in Base64 and 48656c6c6f20576f726c64 in hex. TC39’s proposal documentation also shows conversion from a typed-array view, so calling the method on a view converts that view’s bytes rather than every byte in its larger backing buffer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why avoid the btoa() round trip?

btoa() accepts a binary string: each character must represent a single byte. It does not accept a Uint8Array as its byte-oriented input, and it is not a general Unicode-to-Base64 function. Passing characters with code points above 255 can throw an InvalidCharacterError. MDN’s btoa() documentation explains this constraint.

A common workaround turns each byte into a string character, then calls btoa(). That creates an intermediate string whose characters stand for bytes—not ordinary text. If the data is already in a byte array, toBase64() performs the conversion in the byte-array API and avoids that detour. The available documentation establishes this API distinction, not a quantified speed improvement.

Choose the right encoding for text

Text encoding and Base64 encoding are separate operations. First convert Unicode text into bytes using the intended character encoding; for UTF-8, use TextEncoder. Then convert those bytes to Base64:

const text = "Hello, world";
const bytes = new TextEncoder().encode(text);
const base64 = bytes.toBase64();

Here, TextEncoder produces UTF-8 bytes, and toBase64() represents those bytes as Base64. If your input is already bytes—for example, data read from a file or received from an API—do not convert it to text first unless you specifically need to interpret it as text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set Base64 alphabet and padding to match the receiver

By default, toBase64() uses the standard Base64 alphabet and includes padding when needed. The URL-safe alphabet replaces + and / with - and _. Some receiving formats also require omitted trailing padding.

const standard = bytes.toBase64();
const urlSafe = bytes.toBase64({ alphabet: "base64url" });
const unpadded = bytes.toBase64({ omitPadding: true });
const urlSafeUnpadded = bytes.toBase64({
  alphabet: "base64url",
  omitPadding: true,
});

Choose the alphabet and padding rules required by the protocol or format consuming the string; they are not interchangeable just because both forms encode the same bytes. See MDN’s toBase64() reference for the method’s options.

Decode Base64 into bytes

For the reverse operation, Uint8Array.fromBase64() returns a byte array:

const decoded = Uint8Array.fromBase64(base64);

The decoder ignores ASCII whitespace; its options can also control alphabet handling and how an incomplete final chunk is treated. If you need to place decoded bytes into an existing preallocated array, Uint8Array.prototype.setFromBase64() is the corresponding in-place option. Check the MDN fromBase64() reference and the TC39 proposal documentation for the relevant details.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check support in your target runtime

The TC39 proposal repository says the proposal reached Stage 4 and is included in the standard. Standardization does not mean every older browser or pinned runtime implements the methods. MDN marks toBase64() and fromBase64() Baseline 2025, with support across the latest browser and device versions from September 2025, while cautioning that older environments may not support them. Treat that as a compatibility signal, not a guarantee for every embedded browser or server runtime.

If your project must run in older environments, check its actual support matrix and provide an appropriate fallback or polyfill. For Node.js code that uses Node’s APIs, follow the Node.js Buffer documentation: it recommends Buffer.from(str, 'base64') and buf.toString('base64') for Base64 conversion, and identifies buffer.atob() and buffer.btoa() as legacy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.