function _arrayBufferToBase64( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return window.btoa( binary );
}

but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6

jsPerf.com is jsPerf.app now: https://jsperf.app/encoding-xhr-image-data/51

Updated benchmarks: https://jsben.ch/wnaZC

Answer from mobz on Stack Overflow
🌐
GitHub
gist.github.com › jonleighton › 958841
Encode an ArrayBuffer as a base64 string · GitHub
I fixed it here https://www.measurethat.net/Benchmarks/Show/10470/0/stringfromcharcode-btoa-vs-base64arraybuffer-function-f turns out that base64 encoding an arraybuffer to string is faster than creating a DOMString then base64 encoding to a DOMstring.
🌐
GitHub
gist.github.com › Deliaz › e89e9a014fea1ec47657d1aac3baa83c
Convert array buffer to base64 string · GitHub
Convert array buffer to base64 string · Raw · arrayBufferToBase64.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Discussions

Base64 encode ArrayBuffer
I have a set of bytes in an ArrayBuffer. I need to base64 encode this in k6. How do I accomplish this? In node.js it is data.toString(‘base64’) in browsers the btoa() function seems to be the way to go. Neither of these works in k6, or I did not manage to get them to work. More on community.grafana.com
🌐 community.grafana.com
3
0
December 7, 2020
How can I convert a base64 string to ArrayBuffer?
I need to convert a base64 encoding string into an ArrayBuffer. The base64 strings are user input, they will be copy and pasted from an email, so they're not there when the page is loaded. I would ... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert an ArrayBuffer to base64url and vice versa?
Somethings are one way hashes and you are not able to reverse them. I don’t know if that is the issue here though. More on reddit.com
🌐 r/learnjavascript
3
3
November 11, 2024
Convert ArrayBuffer to Base64
this one looks badass https://www.npmjs.com/package/react-native-quick-base64 More on reddit.com
🌐 r/reactnative
8
3
April 17, 2024
🌐
Isummation
isummation.com › home › development › convert arraybuffer to base64 string and vice-versa
JavaScript - Convert arrayBuffer to Base64 string and vice-versa iSummation - USA
August 30, 2019 - function base64ToArrayBuffer(base64) { var binary_string = window.atob(base64); var len = binary_string.length; var bytes = new Uint8Array( len ); for (var i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } return bytes.buffer; } The atob() method used to decode a base-64 encoded string...
🌐
Jsben
jsben.ch › convert-arraybuffer-to-base64-string-yduu9
Convert ArrayBuffer to Base64 string - JSBEN.CH JavaScript Benchmark
This benchmark tests different algorithms for converting an ArrayBuffer to a Base64 string.
🌐
Medium
medium.com › @koteswar.meesala › convert-array-buffer-to-base64-string-to-display-images-in-angular-7-4c443db242cd
Convert Array buffer to base64 string to display images in Angular 7 | by Kotesh Meesala | Medium
June 9, 2019 - You may need to make changes to the data using some standard JavaScript built-in methods to convert the ArrayBuffer to base64 string and make use of DomSanitizerlink API in angular to bypass the Angular’s built-in sanitization.
🌐
Grafana
community.grafana.com › grafana k6 › oss support
Base64 encode ArrayBuffer - OSS Support - Grafana Labs Community Forums
December 7, 2020 - I have a set of bytes in an ArrayBuffer. I need to base64 encode this in k6. How do I accomplish this? In node.js it is data.toString(‘base64’) in browsers the btoa() function seems to be the way to go. Neither of the…
Find elsewhere
🌐
xjavascript
xjavascript.com › blog › arraybuffer-to-base64-encoded-string
How to Efficiently Convert ArrayBuffer to Base64 Encoded String for Multipart Posts (Native Method) — xjavascript.com
Chunk the Buffer: Process the ArrayBuffer in smaller chunks (e.g., 1MB at a time) and concatenate the Base64 results. Use Web Workers: Offload the conversion to a Web Worker to prevent UI freezes.
🌐
Jsben
jsben.ch › convert-arraybuffer-to-base64-6qulk
Convert ArrayBuffer to Base64 - JSBEN.CH JavaScript Benchmark
The provided code snippets are designed to convert an ArrayBuffer to a Base64 encoded string. This conversion process is essential for encoding binary data for safe transmission over text-based protocols (like JSON or HTTP).
🌐
Technocat Games
technocatgames.com › blog › fast-arraybuffer-to-base64-in-javascript
Fast ArrayBuffer to Base64 in JavaScript - Technocat Games
December 19, 2024 - const randomBytes = new Uint8Array(50000000); // 50MB for (let i = 0; i < randomBytes.length; i++) { randomBytes[i] = Math.floor(Math.random() * 256); } const buffer = randomBytes.buffer; function measureExecutionTime(fn) { const start = performance.now(); const result = fn(); const end = performance.now(); console.log(`${(end - start).toFixed(2)} ms`); return result; } measureExecutionTime(() => arrayBufferToBase64(buffer)); // Firefox: 860.00 ms // Chrome: 4575.80 ms · Contrary to what you might expect, Firefox did it a whopping 5.3 times faster. Even then, almost a second is way too much for simply converting to Base64. It turns out that concatenating a string 50 million times is not efficient.
🌐
Italypaleale
italypaleale.github.io › arraybuffer › modules › base64.html
base64 | arraybuffer-encoding-encode - v1.1.0
Decode a string from base64, using base64 "URL-safe" encoding. Padding is always optional. ... Encode an ArrayBuffer to base64 in a string, using base64 "URL-safe" encoding.
🌐
Nutn
nutn.page › blog › converting-arraybuffer-to-base64url
Javascript ArrayBuffer to URLEncodedBase64 - Ratthanan N.
These characters can be represented in JS string as JS string encoding is UTF-16 (or UCS2 depending on implementation). Next, we use btoa() to convert our binaryString into Base64 encoded string and save it into the base64EncodedString variable.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer. The ArrayBuffer() constructor creates a new ArrayBuffer of the given length in bytes. You can also get an array buffer from existing data, for example, from a Base64 string or from a local file.
🌐
Reddit
reddit.com › r/learnjavascript › how do i convert an arraybuffer to base64url and vice versa?
r/learnjavascript on Reddit: How do I convert an ArrayBuffer to base64url and vice versa?
November 11, 2024 -

I want to convert

[146, 158, 30, 105, 37, 64, 188, 176, 151, 69, 135, 48, 116, 103, 158, 180, 180, 93, 233, 205, 246, 73, 21, 84, 115, 104, 123, 243, 69, 20, 98, 208]

into

kp4eaSVAvLCXRYcwdGeetLRd6c32SRVUc2h780UUYtA
and vice versa.

I know I can convert the ArrayBuffer into a base64url string because this is a value from a toJSON() function from Web Authentication: An API for accessing Public Key Credentials - Level 3. But I don't know how to convert it back.

🌐
Italypaleale
italypaleale.github.io › arraybuffer
arraybuffer-encoding-encode - v1.1.0
import {Encode, Decode} from 'arraybuffer-encoding/base64/url' In all cases, the Decode function accepts strings with or without padding. To encode an ArrayBuffer to base64, use the Encode(ab: ArrayBuffer): string method:
🌐
npm
npmjs.com › package › base64-arraybuffer
base64-arraybuffer - npm
January 22, 2022 - The library encodes and decodes base64 to and from ArrayBuffers · encode(buffer) - Encodes ArrayBuffer into base64 string · decode(str) - Decodes base64 string to ArrayBuffer · You can run the test suite with: npm test · Copyright (c) 2012 ...
      » npm install base64-arraybuffer
    
Published: Jan 22, 2022
Version: 1.0.2
🌐
TC39
tc39.es › proposal-arraybuffer-base64
Proposed Support for Base64 in JavaScript
let array = new Uint8Array([251, 255, 191]); console.log(array.toBase64({ alphabet: 'base64' })); // '+/+/' console.log(array.toBase64({ alphabet: 'base64url' })); // '-_-_' console.log(Uint8Array.fromBase64('SGVsbG8g\nV29ybG R')); // works, despite whitespace, missing padding, and non-zero overflow bits try { Uint8Array.fromBase64('SGVsbG8gV29ybGR=', { lastChunkHandling: 'strict' }); } catch { console.log('with lastChunkHandling: "strict", overflow bits are rejected'); } try { Uint8Array.fromBase64('SGVsbG8gV29ybGQ', { lastChunkHandling: 'strict' }); } catch { console.log('with lastChunkHandling: "strict", overflow bits are rejected'); } console.log((new Uint8Array([72])).toBase64()); // 'SA==' console.log((new Uint8Array([72])).toBase64({ omitPadding: true })); // 'SA'
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromBase64
Uint8Array.fromBase64() - JavaScript - MDN Web Docs
If the last chunk is exactly 4 characters long with padding = characters, then it's decoded and appended to the result. Otherwise, the last partial chunk is ignored (but if it contains one base64 character followed by =, then a syntax error is still thrown). This is useful if the string is coming from a stream and the last chunk is not yet complete.