Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ArrayBuffer
(version: 0)
Comparing performance of:
Cached decode vs Simple decode
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7aa34a5b6c5acb67acb5acb678acb6ac4b5a6c7b5a4c3b45a67c8b76a54cb34a5c6b7"; 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 _base64ToArrayBufferFaster(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] = lookup[binary_string[i]]; } return bytes.buffer; } 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; }
Tests:
Cached decode
_base64ToArrayBufferFaster(str);
Simple decode
_base64ToArrayBuffer(str);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Cached decode
Simple decode
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and individual test cases. **Benchmark Definition** The benchmark is testing the performance of two functions: `_base64ToArrayBufferFaster` and `_base64ToArrayBuffer`. Both functions take a base64-encoded string as input and return an ArrayBuffer. ```json { "Name": "ArrayBuffer", "Description": null, "Script Preparation Code": "...", ... } ``` The script preparation code sets up some variables: * `str`: a long, base64-encoded string. * `chars`: a string containing all the characters used in the base64 encoding (A-Z, a-z, 0-9, +). * A lookup table (`lookup`) is created to map each character to its index in the `chars` string. **Individual Test Cases** There are two test cases: 1. **Cached decode**: The benchmark definition uses `_base64ToArrayBufferFaster(str);`. This function appears to be optimized for performance, as it uses a lookup table to quickly find the indices of the characters in the input string. 2. **Simple decode**: The benchmark definition uses `_base64ToArrayBuffer(str);`. This function is less optimized than the cached version and simply converts each character to its corresponding index in the `chars` string. **Pros and Cons** * **Cached decode**: + Pros: faster execution time, as it uses a lookup table for optimal performance. + Cons: more complex implementation, which may increase maintenance costs. * **Simple decode**: + Pros: simpler implementation, easier to understand and maintain. + Cons: slower execution time due to the lack of optimization. **Other Considerations** * The benchmark is using `window.atob` to convert the base64-encoded string to a binary string. This function is part of the Web API and provides a convenient way to perform base64 decoding. * The test cases are measuring the performance of the functions in different browsers (Chrome 55). This allows users to compare the performance of each function across different platforms. **Alternatives** If you wanted to create similar benchmarks for other base64 encoding algorithms or approaches, you could consider using: 1. **Other Web API functions**: Instead of `window.atob`, you could use other functions like `btoa` (binary-safe atob) or `atob` with a custom implementation. 2. **Native libraries**: You could use native libraries like libbase64 or base64-js to perform base64 encoding and decoding. 3. **Third-party libraries**: There are many third-party libraries available that provide optimized base64 encoding and decoding implementations. Note that each alternative has its own pros and cons, which would need to be carefully considered when creating a benchmark.
Related benchmarks:
ArrayBuffer
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?