Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array buffer to hex conversion
(version: 0)
convert array buffer to hex
Comparing performance of:
directly set vs push values
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var saltVal = crypto.getRandomValues(new Uint8Array(16)); function arrayBufferToHexString(bytes) { bytes = new Uint8Array(bytes); var hex = new Array(bytes.length); for (let i = 0; i < bytes.length; i++) { hex[i] = ("0" + bytes[i].toString(16)).slice(-2); } return hex.join(""); } function bytesToHexString(bytes) { bytes = new Uint8Array(bytes); var hexBytes = []; for (var i = 0; i < bytes.length; i++) { var byteString = bytes[i].toString(16); if (byteString.length < 2) byteString = "0" + byteString; hexBytes.push(byteString); } return hexBytes.join(""); }
Tests:
directly set
arrayBufferToHexString(saltVal);
push values
bytesToHexString(saltVal)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
directly set
push values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
directly set
1090452.1 Ops/sec
push values
1462441.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is being tested?** The benchmark measures the performance of two approaches for converting an `ArrayBuffer` to a hexadecimal string: 1. **Directly setting**: The code sets the result of `arrayBufferToHexString(saltVal)` directly, without any intermediate steps. 2. **Pushing values**: The code uses the `bytesToHexString(saltVal)` function, which pushes each byte value onto an array before joining them into a string. **Options compared** The benchmark compares two options: 1. **Directly setting**: This approach sets the result directly, without any intermediate steps. 2. **Pushing values**: This approach uses an intermediate step to push each byte value onto an array before joining them into a string. **Pros and cons of each approach:** * **Directly setting**: + Pros: - Simple and straightforward - Fewer memory allocations and copies + Cons: - May require more CPU cycles due to the direct assignment * **Pushing values**: + Pros: - Can be more efficient in terms of memory allocation and copying, especially for large arrays - Allows for easy iteration and processing of each byte value individually + Cons: - Requires an additional step, which can introduce overhead **Library usage** The benchmark uses the `crypto` library to generate a random `Uint8Array` (`saltVal`). The `crypto.getRandomValues()` function is used to generate cryptographically secure random values. **Special JS feature or syntax** None of the test cases use any special JavaScript features or syntax beyond what's standard in modern JavaScript. **Other alternatives** If you wanted to optimize this benchmark further, you could consider using other approaches, such as: * **Using a dedicated hexadecimal conversion library**: Instead of implementing the conversion logic yourself, you could use an existing library that's optimized for performance. * **Using SIMD instructions**: If you're targeting a platform that supports SIMD (Single Instruction, Multiple Data) instructions, you could optimize the conversion logic to use these instructions, which can significantly improve performance for certain types of data. * **Using a Just-In-Time (JIT) compiler**: Some JavaScript engines, like V8 in Chrome, have JIT compilers that can optimize hot functions like this one. You could try running the benchmark through a JIT compiler to see if it improves performance. Keep in mind that these alternatives might add complexity and overhead to your benchmark, so you should carefully consider the trade-offs before implementing them.
Related benchmarks:
parse hex-string to 'bytes like object' (Uint8Array) in javascript
hex to array buffer
array buffer to hex conversion 2
sha1-js-rusha-vs-native-10mb
Comments
Confirm delete:
Do you really want to delete benchmark?