Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ArrayBuffer to base64 String vs. superhuman
(version: 0)
Comparing performance of:
String.fromCharCode.apply vs byteArrayToString vs superhuman
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/gh/superhuman/fast64@master/index.js'></script>
Script Preparation code:
var ab = new ArrayBuffer(70*1024); var uint8 = new Uint8Array(ab); for (var i = 0; i < ab.byteLength; i++) { uint8[i] = i % 256; } var expected = window.btoa(String.fromCharCode.apply(null, uint8)); function byteArrayToString(bytes) { var CHUNK_SIZE = 8*1024; if (bytes.length <= CHUNK_SIZE) return String.fromCharCode.apply(null, bytes); var str = ''; for (var i = 0; i < bytes.length; i += CHUNK_SIZE) str += String.fromCharCode.apply(null, bytes.slice(i, i+CHUNK_SIZE)); return str; } function validate(res){ if (res !== expected) throw "expected=" + expected + " but got " + res; } function uint6ToB64(nUint6) { return nUint6 < 26 ? nUint6 + 65 : nUint6 < 52 ? nUint6 + 71 : nUint6 < 62 ? nUint6 - 4 : nUint6 === 62 ? 43 : nUint6 === 63 ? 47 : 65; }; function bytesToBase64(aBytes) { var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = ""; for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) { nMod3 = nIdx % 3; nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24); if (nMod3 === 2 || aBytes.length - nIdx === 1) { sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63)); nUint24 = 0; } } return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "=="); };
Tests:
String.fromCharCode.apply
var bin = String.fromCharCode.apply(null, uint8); var res = window.btoa(bin) validate(res)
byteArrayToString
var bin = byteArrayToString(uint8); var res = window.btoa(bin) validate(res)
superhuman
var res = B64.encode(uint8); validate(res)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String.fromCharCode.apply
byteArrayToString
superhuman
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.fromCharCode.apply
3910.8 Ops/sec
byteArrayToString
4891.4 Ops/sec
superhuman
3750.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares three different approaches to convert an ArrayBuffer (a binary data type) to a base64 string: 1. `window.btoa(String.fromCharCode.apply(null, uint8))` (using `String.fromCharCode`) 2. `byteArrayToString(uint8)` and then passing it to `window.btoa()` (using a custom function `byteArrayToString`) 3. `B64.encode(uint8)` from the `superhuman` library **Options Compared** The three options are compared in terms of execution time, which is measured as `ExecutionsPerSecond`. The benchmark measures how many times each approach can be executed per second on a specific hardware configuration. **Pros and Cons of Each Approach** 1. **Using `window.btoa(String.fromCharCode.apply(null, uint8))`** * Pros: Simple and straightforward, uses built-in JavaScript functions. * Cons: May not be optimized for performance, may have overhead due to string creation. 2. **Using `byteArrayToString(uint8)` and then passing it to `window.btoa()`** * Pros: Allows for custom optimization of the conversion process, can handle larger ArrayBuffer sizes more efficiently. * Cons: Requires implementing a custom function (`byteArrayToString`) which may add complexity. 3. **Using `B64.encode(uint8)` from `superhuman` library** * Pros: Optimized for performance, uses a dedicated library that is likely to be highly optimized. * Cons: Requires including an external library, may have dependencies or versioning issues. **Library and its Purpose** The `superhuman` library provides a high-performance base64 encoding function (`B64.encode`) that is specifically designed to optimize the encoding process. The library is written in C++ and uses SIMD instructions for optimal performance. **Special JS Feature/Syntax** The benchmark does not explicitly mention any special JavaScript features or syntax, but it does use the `apply()` method to call a function with an array of arguments. This is a common pattern in JavaScript for invoking functions with multiple arguments. **Other Alternatives** Other alternatives for base64 encoding exist, such as using a library like `base64-js` or implementing a custom solution. However, these alternatives may not be optimized for performance like the `superhuman` library.
Related benchmarks:
ArrayBuffer to base64 String
String.fromCharCode & btoa vs base64ArrayBuffer function
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED ON LARGE ARRAY
Comments
Confirm delete:
Do you really want to delete benchmark?