var str = "aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7";
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var encoder = new TextEncoder();
function _base64ToArrayBufferOptimized(base64) {
return encoder.encode(base64);
}
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;
}
_base64ToArrayBufferOptimized(str);
_base64ToArrayBuffer(str);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Encoder | |
Simple |
Test name | Executions per second |
---|---|
Encoder | 5024713.0 Ops/sec |
Simple | 656130.0 Ops/sec |
I'll break down the benchmark and its results in a way that's easy to understand, even for those without deep knowledge of JavaScript.
What is being tested?
The provided JSON represents two microbenchmarks: ArrayBuffer
and two test cases named "Encoder" and "Simple". The tests are designed to measure the performance of converting base64-encoded strings to ArrayBuffer objects.
Options compared
Two options are compared:
TextEncoder
API, which is a modern way to work with Uint8Array and ArrayBuffers in JavaScript.window.atob()
method, which is an older way to decode base64-encoded strings.Pros and Cons of each approach
TextEncoder API (Optimized):
Pros:
Cons:
window.atob()
method:
Pros:
Cons:
Other considerations
str
) as input, which may affect the results.TextEncoder
API is generally faster and more efficient than using window.atob()
.Library used
The TextEncoder
API is used in the optimized function. It's a built-in JavaScript library that provides a way to encode strings into Uint8Array objects.
Special JS feature or syntax
None mentioned, as both test cases use standard JavaScript features.
Benchmark preparation code
The provided script preparation code generates a large base64-encoded string (str
) and defines two functions: _base64ToArrayBufferOptimized
and _base64ToArrayBuffer
. These functions are then used in the benchmark definitions.
Alternatives
If you need to measure performance of converting base64-encoded strings to ArrayBuffers in older browsers, you may want to consider using a library like base64-js
, which provides a more efficient and modern way to work with base64 encoding.
Additionally, if you're interested in exploring other approaches or testing different libraries, you can look into: