Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript Uint8Array to Base64: all
(version: 2)
toBase64 is usually the fastest, but may not be supported by older browsers.
Comparing performance of:
FileReader vs btoa vs Concat vs Spread vs Apply vs Chunk
Created:
4 months ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function bytesToBase64ByFileReader(bytes) { return new Promise(r => { const reader = new FileReader() reader.onload = () => r(reader.result) reader.readAsDataURL(new Blob([bytes])) }).then(url => url.slice(url.indexOf(",") + 1)); } function bytesToBase64ByBtoa(bytes) { return btoa(bytes.reduce((data, byte) => data + String.fromCharCode(byte), "")); } function base64UrlEncodeConcat(bytes) { let binary = ''; for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]); return btoa(binary); } function base64UrlEncodeSpread(bytes) { let binary = String.fromCharCode(...bytes); return btoa(binary); } function base64UrlEncodeApply(bytes) { let binary = String.fromCharCode.apply(null, bytes); return btoa(binary); } function base64UrlEncodeChunk(bytes) { let binaryString = ''; for (let i = 0; i < bytes.length; i += 49152) { // Use chunk size divisible by 3 binaryString += String.fromCharCode.apply(null, bytes.subarray(i, i + 49152)); } return btoa(binaryString); } const bytes1K = new Uint8Array(1024); for (const bytes of [bytes1K]) for (let i = 0; i < bytes.length; i += 65536) crypto.getRandomValues(bytes.subarray(i, i + 65536));
Tests:
FileReader
[Async/Deferred]
await bytesToBase64ByFileReader(bytes1K); deferred.resolve();
btoa
bytesToBase64ByBtoa(bytes1K);
Concat
base64UrlEncodeConcat(bytes1K);
Spread
base64UrlEncodeSpread(bytes1K);
Apply
base64UrlEncodeApply(bytes1K);
Chunk
base64UrlEncodeChunk(bytes1K);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
FileReader
btoa
Concat
Spread
Apply
Chunk
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 OPR/126.0.0.0 (Edition developer)
Browser/OS:
Opera 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
FileReader
3497.0 Ops/sec
btoa
44631.9 Ops/sec
Concat
96851.7 Ops/sec
Spread
28520.8 Ops/sec
Apply
139072.1 Ops/sec
Chunk
138730.0 Ops/sec
Related benchmarks:
ArrayBuffer
ArrayBuffer to base64 String
String.fromCharCode & btoa vs base64ArrayBuffer function
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED
ArrayBuffer to base64 String vs. superhuman
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED ON LARGE ARRAY
JavaScript Uint8Array to Base64: FileReader vs btoa vs toBase64
JavaScript Uint8Array to Base64: btoa vs btoa spread
JavaScript Uint8Array to Base64: btoa vs btoa concat vs btoa concat byte
Comments
Confirm delete:
Do you really want to delete benchmark?