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("");
}
arrayBufferToHexString(saltVal);
bytesToHexString(saltVal)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
directly set | |
push values |
Test name | Executions per second |
---|---|
directly set | 1090452.1 Ops/sec |
push values | 1462441.2 Ops/sec |
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:
arrayBufferToHexString(saltVal)
directly, without any intermediate steps.bytesToHexString(saltVal)
function, which pushes each byte value onto an array before joining them into a string.Options compared
The benchmark compares two options:
Pros and cons of each approach:
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:
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.