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("");
}
function buf2hex(buffer) { // buffer is an ArrayBuffer
return [new Uint8Array(buffer)]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
arrayBufferToHexString(saltVal);
bytesToHexString(saltVal)
buf2hex(saltVal)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
directly set | |
push values | |
buf2hex() |
Test name | Executions per second |
---|---|
directly set | 1717243.1 Ops/sec |
push values | 2420165.2 Ops/sec |
buf2hex() | 1190469.1 Ops/sec |
In this benchmark, three different methods are compared for converting an ArrayBuffer
to a hexadecimal string. Each method has its own approach for iterating through the buffer and constructing the hexadecimal representation, allowing for a performance comparison of these different implementations.
arrayBufferToHexString
:
ArrayBuffer
to a hexadecimal string by creating a new Uint8Array
from the passed bytes
, looping through each byte, converting it to a string representation in hexadecimal, and joining them together.slice
method on the string for padding.bytesToHexString
:
ArrayBuffer
to a hexadecimal string. However, it uses an array (hexBytes
) to collect the hexadecimal strings before joining them at the end.slice
), making the addition of padding explicit.buf2hex
:
Uint8Array
directly into an array and maps over it to create the hexadecimal strings. The padStart
method is used for padding the strings to maintain the correct length.map
).The latest benchmark results show the executions per second for each method run in the Chrome 131 environment:
push values
(bytesToHexString): 3,319,517.25 executions/seconddirectly set
(arrayBufferToHexString): 2,392,962.5 executions/secondbuf2hex()
: 1,645,414.375 executions/secondFrom this data, we can deduce that the bytesToHexString
method performed the best, followed by arrayBufferToHexString
, with buf2hex
being the least performant. This suggests that the way the Uint8Array
values are aggregated significantly impacts performance.
Uint8Array
: All three methods rely on Uint8Array
, which is a typed array that accommodates raw binary data efficiently, ideal for handling binary data such as that found in ArrayBuffer
.Buffer
(Node.js) or libraries like he
(for encoding/decoding HE hexadecimal strings), especially when handling larger datasets or requiring additional encoding features.ArrayBuffer
sizes.In conclusion, choosing the right conversion method depends on the specific context and constraints of the application, including performance requirements, readability, and maintainability.