Script Preparation code:
x
 
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('');
}
function hex(arrayBuffer)
{
    const asciiCodes = new Uint8Array(
        Array.prototype.map.call(
            "0123456789abcdef",
            char => char.charCodeAt()
        )
    );
  
    const buff = new Uint8Array(arrayBuffer);
    const charCodes = new Uint8Array(buff.length * 2);
    for (let i = 0; i < buff.length; ++i)
    {
        charCodes[i * 2] = asciiCodes[buff[i] >>> 4];
        charCodes[i * 2 + 1] = asciiCodes[buff[i] & 0xf];
    }
    return String.fromCharCode(...charCodes);
}
function hex2(arrayBuffer)
{
    return Array.prototype.map.call(
        new Uint8Array(arrayBuffer),
        n => n.toString(16).padStart(2, "0")
    ).join("");
}
Tests:
  • directly set

     
    arrayBufferToHexString(saltVal);
  • push values

     
    bytesToHexString(saltVal)
  • buf2hex()

     
    buf2hex(saltVal)
  • precomputed ASCII with shift

     
    hex(saltVal)
  • array prototype map

     
    hex2(saltVal)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    directly set
    push values
    buf2hex()
    precomputed ASCII with shift
    array prototype map

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131 on Windows
View result in a separate tab
Test name Executions per second
directly set 2491597.0 Ops/sec
push values 3310311.5 Ops/sec
buf2hex() 1640268.4 Ops/sec
precomputed ASCII with shift 861354.1 Ops/sec
array prototype map 1204383.1 Ops/sec