var str = "aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7";
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Use a lookup table to find the index.
var lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
function decode(base64) {
var bufferLength = base64.length * 0.75,
len = base64.length, i, p = 0,
encoded1, encoded2, encoded3, encoded4;
if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i+=4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i+1)];
encoded3 = lookup[base64.charCodeAt(i+2)];
encoded4 = lookup[base64.charCodeAt(i+3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
};
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;
}
decode(str);
_base64ToArrayBuffer(str);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Cached decode | |
Simple decode |
Test name | Executions per second |
---|---|
Cached decode | 350887.1 Ops/sec |
Simple decode | 1617973.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explain what's being tested on MeasureThat.net.
Benchmark Definition Json
The provided JSON represents a benchmark for testing the performance of two functions: decode
and _base64ToArrayBuffer
. The decode
function is responsible for decoding a base64-encoded string, while _base64ToArrayBuffer
simply converts a base64-encoded string to an array buffer.
Options Compared
There are two options being compared:
_base64ToArrayBuffer
function, which is implemented using the atob()
method in modern browsers.decode
function, which appears to be a cached version of the original decode
function.Pros and Cons
Here are some pros and cons of each approach:
atob()
method, which might not be as efficient as a custom implementation.Library and Purpose
The atob()
method is a built-in function in modern browsers that converts a base64-encoded string to a binary string. It's used in the _base64ToArrayBuffer
function to perform the conversion.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark, except for the use of atob()
which is a built-in method in modern browsers.
Other Considerations
When dealing with browser-specific code like this, it's essential to consider factors such as:
Alternatives
If you're looking for alternative approaches to measure base64 decoding performance, here are a few options:
base64-js
that provide optimized implementations of base64 decoding algorithms.In summary, the benchmark on MeasureThat.net compares two approaches to base64 decoding: using the _base64ToArrayBuffer
function and implementing a cached version of the decode
function. While there are pros and cons to each approach, using a custom implementation with caching can potentially lead to better performance.