Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
bytesToHex
(version: 0)
Comparing performance of:
bytes_to_hex vs bytesToHex vs toHexString
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; var count = 100000; for(var i = 0; i<count; i++) { arr.push(i%255); } function bytes_to_hex ( arr ) { var str = ''; for ( var i = 0; i < arr.length; i++ ) { var h = ( arr[i] & 0xff ).toString(16); if ( h.length < 2 ) str += '0'; str += h; } return str; } function bytesToHex(bytes) { for (var hex = [], i = 0; i < bytes.length; i++) { hex.push((bytes[i] >>> 4).toString(16)); hex.push((bytes[i] & 0xF).toString(16)); } return hex.join(""); } function toHexString(byteArray) { return byteArray.map(function(byte) { return ('0' + (byte & 0xFF).toString(16)).slice(-2); }).join('') }
Tests:
bytes_to_hex
bytes_to_hex(arr);
bytesToHex
bytesToHex(arr);
toHexString
toHexString(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
bytes_to_hex
bytesToHex
toHexString
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
bytes_to_hex
625.2 Ops/sec
bytesToHex
245.5 Ops/sec
toHexString
219.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data for the MeasureThat.net benchmark. **Benchmark Definition:** The benchmark definition is a JavaScript function that converts an array of bytes to a hexadecimal string. There are three variants of this function: 1. `bytes_to_hex(arr)`: This function uses a simple loop to iterate over each byte in the input array, convert it to hexadecimal using ASCII codes, and concatenate the results. 2. `bytesToHex(bytes)`: This function is similar to the first one but uses bitwise operations to extract the high and low bytes of each integer value, then converts them to hexadecimal. 3. `toHexString(byteArray)`: This function uses the `map()` method to apply a transformation to each byte in the input array, which extracts the high and low bytes, converts them to hexadecimal, and concatenates the results. **Options Compared:** The three variants of the `bytes_to_hex` function are compared in terms of execution speed. The MeasureThat.net benchmark measures the time it takes for each variant to execute a certain number of times (in this case, 100,000). **Pros and Cons:** * **`bytes_to_hex(arr)`**: This function is simple and easy to understand. However, it may be slower than the other two variants due to the overhead of using string concatenation. * **`bytesToHex(bytes)`**: This function uses bitwise operations, which can be faster than string concatenation. However, it requires a basic understanding of bitwise operations and may be less readable than the first variant. * **`toHexString(byteArray)`**: This function is concise and efficient, but it relies on the `map()` method, which may incur additional overhead compared to simple loops. **Other Considerations:** * **Memory Allocation:** The `bytes_to_hex(arr)` function allocates memory for a string variable (`str`) each time it's called. In contrast, the `bytesToHex(bytes)` and `toHexString(byteArray)` functions do not allocate explicit memory. * **Browser Support:** All three variants should work in modern browsers, but their behavior may vary due to differences in how they handle strings and bitwise operations. **Library Usage:** None of the benchmarked functions use any external libraries. However, if we were to write more complex benchmarks, we might consider using libraries like `console.time()` or `performance.now()` for timing measurements, or `string-escape` library for string escaping (although this is not used in the provided benchmark). **Special JS Features/Syntax:** There are no special JavaScript features or syntax used in these functions beyond basic arithmetic and loop constructs. The only notable feature is the use of bitwise operations (`&`, `>>>`) in the `bytesToHex(bytes)` function, which may require a brief explanation for developers unfamiliar with this syntax. **Alternatives:** If you wanted to write similar benchmarks using different approaches, you could try: * Using a library like `benchmark.js` or `testify.js` that provides more advanced benchmarking features. * Implementing the functions in a different language (e.g., Python) and then translating them into JavaScript for comparison. * Using online tools like `jsperf` or `jsbench` to compare execution speeds.
Related benchmarks:
bytesToHex
bytesToHex
parse hex-string to 'bytes like object' (Uint8Array) in javascript
array buffer to hex conversion
Comments
Confirm delete:
Do you really want to delete benchmark?