Conversation
|
Hi, sorry for appearing out of nowhere. I happen to have subscribed to all PRs of this repo because I'm personally interested in this repo, so I saw this PR. I also happen to have researched arrayBuffer <-> Base64 conversion before, so I have one thing to comment. So back in 2023, because of a project I was doing, I benchmarked all browser implementations of arrayBuffer <-> Base64, and found that base64→arrayBuffer is fastest when done in js, but arrayBuffer→base64 is 10x faster when done with the FileReader API. Here's the code, I've been using it in production since 2023: /** convert arrayBuffer to base64 using the fileReader API,
* much more performant than btoa or js implementation
* @param {ArrayBuffer} buffer
* @returns {Promise<string>}
*/
export async function arrayBufferToBase64(buffer) {
const file = new File([buffer], "temp.bin", {
type: "application/octet-stream",
});
const fileReader = new FileReader();
const dataUrl = await new Promise((resolve) => {
fileReader.onload = () => resolve(fileReader.result);
fileReader.readAsDataURL(file);
}); // something like `data:application/octet-stream;base64,AQpy3Es9Ja0Abb=`
return dataUrl.split("base64,")[1];
}Would someone like to benchmark it? (one thing to note: my benchmark was done with larger data (10MB-100MB) because that's what my project involved) |
|
@ZYinMD FileReader is not available in Node, it'd also require the function to be async so it'd be entirely unsuitable in this case. Here is a benchmark with the more "native" (but deprecated) atob/btoa: Encoding
Decoding
|
|
I also tested btoa and bota back then, and despised them - btoa would throw error on input larger than ~1MB, and atob is 10x slower than js implementation. Not sure why they did ok in your benchmark, maybe because the data wasn't large, but I remember they sucked at 10MB-100MB input. Anyway, I thought on the server side some c++ library could be imported as dependency, and on the browser side async shouldn't be a problem. But what do I know? |
|
|
Thank you. Out of curiosity I opened #107; haven't properly compared the two but it seems plausible that it would yield good results |
Less common but neither is human readable, the following benchmarks are of the current implementation in comparison to the one in this pull request:
Size Comparison
Encoding Performance
Decoding Performance