Last active
October 11, 2022 17:49
-
-
Save fonsp/a4a2d127f35e72deec14227c1bdf3d04 to your computer and use it in GitHub Desktop.
This file contains 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.
Learn more about bidirectional Unicode characters
const base64_arraybuffer = async (data) => { | |
// Use a FileReader to generate a base64 data URI | |
const base64url = await new Promise((r) => { | |
const reader = new FileReader() | |
reader.onload = () => r(reader.result) | |
reader.readAsDataURL(new Blob([data])) | |
}) | |
/* | |
The result looks like | |
"data:application/octet-stream;base64,<your base64 data>", | |
so we split off the beginning: | |
*/ | |
return base64url.split(",", 2)[1] | |
} | |
// example use: | |
await base64_arraybuffer(new Uint8Array([1,2,3,100,200])) |
This file contains 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.
Learn more about bidirectional Unicode characters
let measure = async () => { | |
// create some random data in a small buffer | |
let smallbuffer = new Uint8Array(50000) | |
crypto.getRandomValues(smallbuffer) | |
// create a large buffer (10MB) with this random data | |
let length = 10000000 | |
let buffer = new Uint8Array(length) | |
buffer.forEach((_,i) => { buffer[i] = smallbuffer[i % 50000] }) | |
// call our function | |
let start = performance.now() | |
await base64_arraybuffer(buffer) | |
let stop = performance.now() | |
// return a measurement | |
let seconds = (stop - start) / 1000 | |
let megabytes = length / 1000 / 1000 | |
return `MB per second: ${Math.floor(megabytes / seconds)}` | |
} | |
// example use: | |
await measure() | |
// returns: 'MB per second: 261' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment